0

I need to call the filename and set to ImageView but it does not working at all. I knew the code below is totally wrong:

holder.iconFilename.setImageDrawable(subjects.getIconFilename());

How to call it correctly? All the images that need to be call were in Drawable folder.


My full code:

@Override
public void onBindViewHolder(SubjectAdapter.MyViewHolder holder, int position) {
    context = holder.itemView.getContext();
    Subjects subjects = subjectsList.get(position);
    holder.iconFilename.setImageDrawable(subjects.getIconFilename()); // Definitely wrong.
    holder.subjectNameStd.setText(subjects.getSubjectNameStd());
    holder.itemView.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Intent gotoChapter = new Intent (context,ChapterActivity.class);
            context.startActivity(gotoChapter);
        }
    });
}
Nere
  • 4,097
  • 5
  • 31
  • 71

1 Answers1

0

If you mean that you have images as resources like this: res/drawable/whatever, then you load it into the ImageView like this:

imageView.setImageResource(R.drawable.whatever);

That's a pretty standard text book thing. You are probably missing something else.

For example, your holder.iconFilename look suspicious. Your holder should have a property that's an ImageView, not a file name, e.g. holder.fooImageView.

The other potential problem that you are not explaining is what Subjects is. It should return the resource id, e.g. subjects.getPhotoResourceId(). If you are trying to load your refernences to the drawable resource from a database or other external source, you need to create a mapping from an external constant to the R.drawable.whatever constant.

For example:
if (subjects.getStatusId() == OK_STATUS) {
  holder.statusImageView.setImageResource(R.drawable.ok_icon);
}
Thomas Fischer
  • 1,394
  • 2
  • 12
  • 25