I know there are questions exactly like mine. I have looked at a couple and I have been using this one: How can I convert String to Drawable . However when I use this code from this answer in my code drawable is always null.
His code:
int id = getResources().getIdentifier(name, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
Here is my code where this keeps happening. The image view is in the right spot but there is no image there. When I run it with the debugger, the variable id gets what I think is the right integer value but I do not know of a way to confirm it.
String[] images = currExercise.get_allImg().split(",");
int id = context.getResources().getIdentifier(images[0], "drawable", context.getPackageName());
Drawable drawable = context.getResources().getDrawable(id);
mainImage.setBackground(drawable);
Is there any alternative to doing this? Any suggestions is greatly appreciated and Thank you.
I am adding my entire Adapter class
public class ExerciseAdapter extends BaseAdapter {
//song list and layout
private ArrayList<Exercise> exercises;
private LayoutInflater exerciseInf;
private Context context;
private int screenWidth;
I am passing in the context. Is this what you mean?
//constructor
public ExerciseAdapter(Context c, ArrayList<Exercise> allExercises, int sw){
exercises=allExercises;
exerciseInf=LayoutInflater.from(c);
context = c;
screenWidth = sw;
}
@Override
public int getCount() {
return exercises.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//map to song layout
LinearLayout exerciseLay = (LinearLayout)exerciseInf.inflate(R.layout.exercise, parent, false);
//get title and artist views
ImageView exerciseMainImage = (ImageView)exerciseLay.findViewById(R.id.exerciseMainImage);
exerciseMainImage.getLayoutParams().width = (int)Math.floor(screenWidth * .3);
exerciseMainImage.getLayoutParams().height = (int)Math.floor(screenWidth * .3);
LinearLayout exerciseContent = (LinearLayout)exerciseLay.findViewById(R.id.exerciseContent);
exerciseContent.getLayoutParams().width = (int)Math.floor(screenWidth * .7);
TextView titleView = (TextView)exerciseContent.findViewById(R.id.exerciseName);
TextView entryView = (TextView)exerciseContent.findViewById(R.id.exerciseDescription);
Exercise currExercise = exercises.get(position);
String[] images = currExercise.get_allImg().split(",");
/*Original*///exerciseMainImage.setImageResource(context.getResources().getIdentifier(images[0], "drawable", context.getPackageName()));
int id = context.getResources().getIdentifier(images[0], "drawable", context.getPackageName());
Drawable drawable = context.getResources().getDrawable(id);
/*Debug*///Drawable drawable = context.getResources().getDrawable(R.drawable.backlifts1);
exerciseMainImage.setBackground(drawable);
titleView.setText(currExercise.get_exerciseName());
entryView.setText(currExercise.get_description());
exerciseLay.setTag(position);
return exerciseLay;
}
}