我有一个数据库,其中存储了一些照片的名称。在 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));
我该如何解决这个问题?先感谢您。