Issue
I have a class called Monster.class which contains String type variables.
Another class MonsterAdapter.class use the getters to get the values and set on MyGridView.
It works fine but only if I set the image manual like this:
imageView.setImageResource(R.drawable.a1);
(a1 is the name of the image)
If I try to use this:
final Monster m = monsters[position];
String variableValue = m.getImg();
imageView.setImageResource(getResources().getIdentifier(variableValue, "drawable", getPackageName()));
It says can't not resolve method "getResources()" and "getPackageName()" and I only can import static method which throws error:
ERROR
Solution
You can define Context inner class MonsterAdapter
public class MonsterAdapter extends BaseAdapter{
Context mContext;
public MonsterAdapter(Context context, ...){
this.mContext = context;
...
}
}
then you can use
imageView.setImageResource(mContext.getResources().getIdentifier(variableValue, "drawable", getPackageName()));
Answered By - nhonnq
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.