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.