我有一个数据库,我存储了一些照片的名称。 在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(渣油));
我该如何解决这个问题? 提前谢谢。
答案 0 :(得分:1)
作为@Lacksprog,您将需要传递给适配器类的构造函数的活动上下文。
你有这个
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
答案 1 :(得分:0)
这是您定义的构造函数。我看到你把它作为一个参数传递给它。自己使用它 PointOfInterestAdapter(Context ctxt,int layout,Cursor c,String [] from,int [] to,int flags){ 超级(ctxt,布局,c,由,于,标志); }
你已经在构造函数中使用了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
}
......................
现在在使用getApplicationContext()