0

Hi I cant find the solutiıon for my question. I take name of the images from db. And then I should give tat string to image.setImageResource. There are lots of answers which include getResources().getIdentifier( But getResoruces() is not work. I get error like: Error:(40, 21) error: cannot find symbol method getResources() How can I solve this problem thanks.

public class ReceivedItemAdapter extends ArrayAdapter<ReceivedItem> {

    public ReceivedItemAdapter(Context context, ArrayList<ReceivedItem> items){
        super(context,R.layout.row_item_received,items);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View customView = LayoutInflater.from(this.getContext())
            .inflate(R.layout.row_item_received, parent, false);

        ReceivedItem item = getItem(position);
        TextView textName = (TextView) customView.findViewById(R.id.nameTextView);
        TextView textCalorie = (TextView) customView.findViewById(R.id.calorieTextView);
        ImageView imageView = (ImageView) customView.findViewById(R.id.imageView);
        textName.setText(item.getName());
        textCalorie.setText(item.getCalorie()+ " cal");
        String mDrawableName = item.getImageName();
        int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
        imageView.setImageResource(resID);
        customView.setTag(item.getId());
        return customView;
    }
}

I use this code in a listview adapter class

Ariel Carbonaro
  • 1,429
  • 1
  • 14
  • 25
berkt
  • 109
  • 1
  • 8

3 Answers3

1

Please post some code, before we can help you.

If you want to access getResources outside and activity or similar, you need to get a reference to context and then call context.getResources.

Just call customView.getContext().getResources()

1

You need to keep the context instance inside your adapter, and then call to context.getResources()

public class ReceivedItemAdapter extends ArrayAdapter<ReceivedItem> {

private final Context context;

public ReceivedItemAdapter(Context context, ArrayList<ReceivedItem> items){
    super(context,R.layout.row_item_received,items);
    this. context = context;
}

public View getView(int position, View convertView, ViewGroup parent) {

    View customView = LayoutInflater.from(this.getContext())
            .inflate(R.layout.row_item_received, parent, false);

    ReceivedItem item = getItem(position);
    TextView textName = (TextView) customView.findViewById(R.id.nameTextView);
    TextView textCalorie = (TextView) customView.findViewById(R.id.calorieTextView);
    ImageView imageView = (ImageView) customView.findViewById(R.id.imageView);
    textName.setText(item.getName());
    textCalorie.setText(item.getCalorie()+ " cal");
    String mDrawableName = item.getImageName();
    int resID = context.getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
    imageView.setImageResource(resID);
    customView.setTag(item.getId());
    return customView;
}
Ariel Carbonaro
  • 1,429
  • 1
  • 14
  • 25
1

Both answers are correct and work.

But it is easier to call

 int resID = customView.getContext().getResources().getIdentifier(mDrawableName , "drawable", getPackageName());

You can do this, since every view has a reference to the context

public final Context getContext ()

Returns the context the view is running in, through which it can access the current theme, resources, etc.

Returns Context The view's Context.

Chris Sherlock
  • 903
  • 5
  • 19
  • I get know ID: 7614 java.lang.OutOfMemoryError: Failed to allocate a 17640012 byte allocation with 5184936 free bytes and 4MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) – berkt May 10 '16 at 19:45
  • This is an other Problem, but probably your Images you try to load are to big – Chris Sherlock May 10 '16 at 19:54
  • thank you I handle that problem by making them smaller. Is there any solution instead of making smaller? – berkt May 10 '16 at 20:00
  • 1
    there are several Solutions : http://stackoverflow.com/questions/25719620/how-to-solve-java-lang-outofmemoryerror-trouble-in-android – Chris Sherlock May 10 '16 at 20:05