我有一個數據庫,我存儲了一些照片的名稱。 在onCreate()方法我提出一個新的PointofInterestAdapter:Android:調用getApplicationContext時的上下文錯誤()
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(光標C,DatabaseConnector databaseConnector) 圖像
問題是,我得到該錯誤消息:
「不能使靜態參考從類型ContextWrapper的非靜態方法getApplicationContext()」
在行:。
INT渣油= getApplicationContext()getResources()則getIdentifier(PHOTO_NAME,「 drawable「,getApplicationContext()。getPackageName());
和這裏:(。getApplicationContext()getResources()getDrawable(渣油))
icon.setImageDrawable;
我該如何解決這個問題? 預先感謝您。
在'NewView的簡單的傳遞'context'參考你的'PointOfInterestHolder'類()'方法和使用。另外,你的視圖有一個''Context',你可以通過'view.getContext()'獲得。 – Luksprog
謝謝你的回答。你能更具體一點嗎?我怎樣才能做到這一點? –
在'newView'方法中使用:'PointOfInterestHolder holder = new PointOfInterestHolder(row,ctxt);'。還要更改'PointOfInterestHolder'類:'... private Context mContext; PointOfInterestHolder(View row,Context context){mContext = context; ...'。然後你可以使用'mContext'來獲取資源。 – Luksprog