0

为了这

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

我得到了错误

此行有多个标记 - 未为 MenuAdapter 类型定义方法 getPackageName() - 无法从类型 Context 对非静态方法 getResources() 进行静态引用 - 无法解析上下文

我真的不知道如何解决这个问题。

这是我的课。目标是通过 .setRecourse(int) 更改 ImageView 中的图像

 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;
    }

}
4

4 回答 4

4

编辑:因为你有一个活动参考,你可以使用它

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

尝试这样调用

 int identifier = getActivity().getResources().getIdentifer(Index.THUMBNAIL, "drawable", getPackageName())
于 2013-02-27T12:11:26.860 回答
2

试试下面的代码:

 Context.getResources().getIdentifer(Index.THUMBNAIL, "drawable",
 YourActivityName.class.getPackage().getName());
于 2013-02-27T12:16:41.663 回答
0

如果 getApplicationContext() 不起作用,这就是我的做法:

int id = getResources().getIdentifier("packagename:drawable/" + drawableresourcename, null, null);
image.setImageResource(id);
于 2013-02-27T12:31:23.157 回答
0

只需context在调用之前添加getResources()context.getResources()

于 2018-01-17T19:24:31.450 回答