Reputation: 415
I have a database where i store the names of some photos. At onCreate() method i make a new 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);
Here is the code for making the 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);
}
}
}
at populateForm(Cursor c, DatabaseConnector databaseConnector) i try to set the image
The problem is that i get that error message:
"Cannot make a static reference to the non-static method getApplicationContext() from the type ContextWrapper"
at lines:
int resID = getApplicationContext().getResources().getIdentifier(photo_name, "drawable", getApplicationContext().getPackageName());
and here:
icon.setImageDrawable(getApplicationContext().getResources().getDrawable(resID));
How can i solve that problem ? Thank you in advance.
Upvotes: 0
Views: 1556
Reputation: 133560
As @ Luksprog you will need the activity context which is passed to the constructor of your adapter class.
You have this
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;
}
Then use the context
int resID = mContext.getResources().getIdentifier(photo_name, "drawable", mContext.getPackageName());
Note
Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
More info @
http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html
Upvotes: 1
Reputation: 27549
This is the constructor you defined. I see you are passing context in it as a param. Use it itself PointOfInterestAdapter(Context ctxt, int layout, Cursor c, String[] from, int[] to, int flags) { super(ctxt,layout,c,from,to,flags); }
you have ctxt/Context already in the constructor, Declare a local variable and store this ctxt in that. like below
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
}
......................
now use mCtx where ever you using getApplicationContext()
Upvotes: 0