user2108957
user2108957

Reputation:

getResources() error

for this

// Look for the image
        int identifier = Context.getResources().getIdentifer(Index.THUMBNAIL, "drawable", getPackageName());

i got the error

Multiple markers at this line - The method getPackageName() is undefined for the type MenuAdapter - Cannot make a static reference to the non-static method getResources() from the type Context - context cannot be resolved

I really don't know how to fix this.

Here is my class. The goal is to change an image in an ImageView by .setRecourse(int)

 package com.example.whs;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuAdapter extends BaseAdapter{
    // Define variables
    ArrayList<HashMap<String, String>> data;
    Activity activity;
    private LayoutInflater inflater=null;

    public MenuAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data = d;
        inflater = LayoutInflater.from (a);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        // Focus on the parts that have to be changed
        TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView subtitle = (TextView)vi.findViewById(R.id.subtitle); // subtitle
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

        //Get the info from the hashmap with the arraylist position
        HashMap<String, String> item = new HashMap<String, String>();
        item = data.get(position);

        // Look for the image
        int identifier = getApplicationContext().getResources().getIdentifer(Index.THUMBNAIL, "drawable", MenuAdapter.class.getPackage().getName());;

        // Setting all values in listview
        title.setText(item.get(Index.TITLE));
        subtitle.setText(item.get(Index.SUBTITLE));
        thumb_image.setImageResource(identifier);
        return vi;
    }

}

Upvotes: 0

Views: 18136

Answers (4)

Clement
Clement

Reputation: 35

Just add context before calling getResources() like: context.getResources()

Upvotes: 0

Gyonder
Gyonder

Reputation: 3754

Try the following code :

 Context.getResources().getIdentifer(Index.THUMBNAIL, "drawable",
 YourActivityName.class.getPackage().getName());

Upvotes: 2

SingleWave Games
SingleWave Games

Reputation: 2648

if getApplicationContext() does not work, this is how I am doing it:

int id = getResources().getIdentifier("packagename:drawable/" + drawableresourcename, null, null);
image.setImageResource(id);

Upvotes: 0

Pragnani
Pragnani

Reputation: 20155

Edit: As you have a activity reference you can use that

activity.getResources().getIdentifer(Index.THUMBNAIL, "drawable", getPackageName())

Try calling like this

 int identifier = getActivity().getResources().getIdentifer(Index.THUMBNAIL, "drawable", getPackageName())

Upvotes: 4

Related Questions