1

I was trying to display images and titles on a card view. The codes should get the image name from a text file and convert it to the images' IDs which the images are already in the drawable file. But when I run my codes on the emulator, only the titles are showing. So I used Log.d to check the values and it turns out the images' names is already there but the resource ID is 0. I've been searching for the same questions and solutions but nothing works. Or is there a better way to retrieve the images' IDs in an adapter?

@Override
public void onBindViewHolder(@NonNull MyRecyclerViewAdapter.MyViewHolder myViewHolder, int i) {

    News news = news_list.get(i);
    myViewHolder.textView_title.setText(news.getNews_title());
    mContext = mContext.getApplicationContext();
    String imageName = news.getImage_name();
    int resID = mContext.getResources().getIdentifier(imageName, "drawable", mContext.getPackageName());
    Log.d("here",news.getImage_name());
    Log.d("here", String.valueOf(resID));
    final String URL = news.getURLs();
    myViewHolder.imageView_image.setImageResource(resID);

Above are the codes in my recyclerviewadapter's onBindViewHolder to display titles and images in the cardview.

2019-11-10 13:09:00.688 29179-29179/com.example.assignment4_task1 D/here: cyber5.png
2019-11-10 13:09:00.689 29179-29179/com.example.assignment4_task1 D/here: 0

2019-11-10 13:09:00.707 29179-29179/com.example.assignment4_task1 D/here: cyber1.png

2019-11-10 13:09:00.707 29179-29179/com.example.assignment4_task1 D/here: 0

2019-11-10 13:09:00.782 29179-29179/com.example.assignment4_task1 D/here: ai1.png

2019-11-10 13:09:00.782 29179-29179/com.example.assignment4_task1 D/here: 0

2019-11-10 13:09:00.799 29179-29179/com.example.assignment4_task1 D/here: ai3.png

2019-11-10 13:09:00.799 29179-29179/com.example.assignment4_task1 D/here: 0

2019-11-10 13:09:00.824 29179-29179/com.example.assignment4_task1 D/here: cyber5.png

2019-11-10 13:09:00.824 29179-29179/com.example.assignment4_task1 D/here: 0

2019-11-10 13:09:00.842 29179-29179/com.example.assignment4_task1 D/here: ai2.png

2019-11-10 13:09:00.842 29179-29179/com.example.assignment4_task1 D/here: 0

2019-11-10 13:09:00.860 29179-29179/com.example.assignment4_task1 D/here: ai4.png

2019-11-10 13:09:00.861 29179-29179/com.example.assignment4_task1 D/here: 0

Above are the results from Logcat. The image names is already there and I didn't purposely put +".png" behind the names but the resource ID still returning 0.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Pepega
  • 25
  • 5
  • Your log message `D/here: ai3.png` says you *are* including the `.png` in your `imageName`. – ianhanniballake Nov 10 '19 at 06:31
  • But in the drawable file, all my images have .png extensions though. Should I remove the .png in the text file that I retrieves all image names from? – Pepega Nov 10 '19 at 06:57
  • Follow this thread https://stackoverflow.com/questions/15488238/using-android-getidentifier – Arul Nov 10 '19 at 07:40

1 Answers1

2

Remove .png from your resource name. getIdentifier works without file extension.

String imageName = news.getImage_name().substring(0, str.lastIndexOf('.'))

If you want to use your file extensions use assets folder (I think in your case its better to use assets folder):

Drawable d = Drawable.createFromStream(getAssets().open("Images/image_name.png"), null);
feridok
  • 648
  • 7
  • 26