0

我有一个数据库,其中存储了一些照片的名称。在 onCreate() 方法中,我创建了一个新的 PointofInterestAdapter:

String[] from=new String[] {"name", "address", "favorite", "type", "distance"};
int[] to = new int[] {R.id.name, R.id.address, R.id.favoriteImage, R.id.icon, R.id.distance};
//SCAdapter = new SimpleCursorAdapter(this, R.layout.row, null, from ,to, 0);   
SCAdapter = new PointOfInterestAdapter(this, R.layout.row_subcategory, null, from ,to, 0);
list.setAdapter(SCAdapter);

以下是制作 SCAdapter 的代码:

class PointOfInterestAdapter extends SimpleCursorAdapter {

    PointOfInterestAdapter(Context ctxt, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(ctxt,layout,c,from,to,flags);
    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
      LayoutInflater inflater=getLayoutInflater();
      View row=inflater.inflate(R.layout.row_subcategory, parent, false);
      PointOfInterestHolder holder=new PointOfInterestHolder(row);
      row.setTag(holder);
      return row;
    }

    @Override
    public void bindView(View row, Context ctxt, Cursor c) {
        PointOfInterestHolder holder=(PointOfInterestHolder)row.getTag();
        holder.populateFrom(c, databaseConnector);
    }
}

static class PointOfInterestHolder {
    private TextView name=null;
    private TextView address=null;
    private ImageView icon=null;
    private ImageView favoriteImage=null;
    private TextView distance=null;

    PointOfInterestHolder(View row) {
        name=(TextView)row.findViewById(R.id.name);
        address=(TextView)row.findViewById(R.id.address);
        favoriteImage=(ImageView)row.findViewById(R.id.favoriteImage);
        icon=(ImageView)row.findViewById(R.id.icon);
        distance=(TextView)row.findViewById(R.id.distance);
    }

    void populateFrom(Cursor c, DatabaseConnector databaseConnector) {
        name.setText(databaseConnector.getName(c));
        address.setText(databaseConnector.getAddress(c));
        distance.setText(databaseConnector.getDistance(c)+" m");

        //---set the image ---
        String photo_name=c.getString(c.getColumnIndex("photo_name"));
        int resID = getApplicationContext().getResources().getIdentifier(photo_name, "drawable",  getApplicationContext().getPackageName());
        //Resources res = getResources();
        //Drawable drawable=res.getDrawable(R.drawable.myimage);
        icon.setImageDrawable(getApplicationContext().getResources().getDrawable(resID));


        //--> set favorite image
        if(databaseConnector.getFavorite(c).equals("yes")) {
            favoriteImage.setImageResource(R.drawable.favorite_yes);
        }
        else if (databaseConnector.getFavorite(c).equals("no")) {
            favoriteImage.setImageResource(R.drawable.favorite_no);
        }
    }
}

在 populateForm(Cursor c, DatabaseConnector databaseConnector) 我尝试设置图像

问题是我收到该错误消息:

“无法从 ContextWrapper 类型对非静态方法 getApplicationContext() 进行静态引用”

在行:

int resID = getApplicationContext().getResources().getIdentifier(photo_name, "drawable", getApplicationContext().getPackageName());

和这里:

icon.setImageDrawable(getApplicationContext().getResources().getDrawable(resID));

我该如何解决这个问题?先感谢您。

4

2 回答 2

1

作为@Luksprog,您将需要传递给适配器类的构造函数的活动上下文。

你有这个

  private Context mContext; 
  PointOfInterestAdapter(Context ctxt, int layout, Cursor c, String[] from, int[] to, int flags) {
    super(ctxt,layout,c,from,to,flags);
    mContext = ctxt;
}

然后使用上下文

 int resID = mContext.getResources().getIdentifier(photo_name, "drawable", mContext.getPackageName());

笔记

不要保留对上下文活动的长期引用(对活动的引用应该与活动本身具有相同的生命周期)

更多信息 @

http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html

于 2013-07-06T08:11:06.343 回答
0

这是您定义的构造函数。我看到您将上下文作为参数传递给它。自己使用 PointOfInterestAdapter(Context ctxt, int layout, Cursor c, String[] from, int[] to, int flags) { super(ctxt,layout,c,from,to,flags); }

您已经在构造函数中有 ctxt/Context,声明一个局部变量并将这个 ctxt 存储在其中。像下面

class PointOfInterestAdapter extends SimpleCursorAdapter {

Static Context mCtx; // local context instance
    PointOfInterestAdapter(Context ctxt, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(ctxt,layout,c,from,to,flags);
    mCtx = ctxt; // assigning context instance in local variable
    }

……………………………………………………………………………………………………………………

现在在您使用的任何地方使用 mCtxgetApplicationContext()

于 2013-07-06T08:09:27.407 回答