Issue
I get a string out of my server request for one item in my recyclerview and it gives me PDF Word or Image. Now I want to display different images in my recyclerviewitem for each String. This is my code:
@Override
public void onBindViewHolder(ScriptViewHolder holder, int position) {
final ItemObject currentScript = scriptObjects.get(position);
holder.scriptName.setText(currentScript.getScriptName());
holder.scriptDate.setText(currentScript.getScriptDate());
holder.scriptUser.setText(currentScript.getScriptUser());
String whichformat = currentScript.getScriptFormat();
switch (whichformat) {
case "PDF":
imagename = "ic_picture_as_pdf_black_24dp.png";
case "Word":
imagename = "ic_insert_drive_file_black_24dp.png";
case "Image":
imagename = "ic_photo_library_black_24dp.png";
}
int resourceId = context.getResources().getIdentifier(imagename, null , null );
holder.scriptIcon.setImageDrawable(ContextCompat.getDrawable(context, resourceId));
I get an error:
FATAL EXCEPTION: main Process: com.ndlp.socialstudy, PID: 3481 android.content.res.Resources$NotFoundException: Resource ID #0x0
and it points to my recycler adapter class and to:
holder.scriptIcon.setImageDrawable(ContextCompat.getDrawable(context, resourceId));
Do you know what I did wrong? The imagenames like in the switch are right I already checked them.
I also already tried:
int resourceId = context.getResources().getIdentifier(imageName, "drawable" , context.getPackageName() );
Kind regards
hellownero
Solution
you should get resourceId with this manner:
switch (whichformat) {
case "PDF":
imagename = "ic_picture_as_pdf_black_24dp";
case "Word":
imagename = "ic_insert_drive_file_black_24dp";
case "Image":
imagename = "ic_photo_library_black_24dp";
}
int resourceId = context.getResources().getIdentifier(imagename, "drawable", context.getPackageName());
You got this exception because you pass null
as defType
and packageName
. you don't need to pass file type as the image name.
Answered By - Ali Abdolahi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.