android - Recyclerview holder setting an image text instead of drawble -
i need display array texts image, displaying them text working, image not working. below explanation.
i have array of texts want display them in recycler view image.
string[] images = { "for_honor.png", "r.drawable.ghost_recon.png", "r.drawable.horizon_zerod.png", "r.drawable.mass_effect.png" };
setting them in way seems wrong not getting image
((myviewholder) holder).getmdataimgview().getresources().getidentifier(name,"drawable","com.example.pkg.pg.adapters");
however code working show me them text not image
// ((myviewholder) holder).getmdatatextview().settext(name);
this code im main activity
maingridrecview = (recyclerview) findviewbyid(r.id.maingridrc); maingridrecviewlayoutmanager = new linearlayoutmanager(context); maingridrecview.setlayoutmanager(maingridrecviewlayoutmanager); maingridrecviewadapter = new maingridrvadapter( datahorizontal,datavertical,mainactivity.this); maingridrecview.setadapter(maingridrecviewadapter);
this maingridrvadapter
public class maingridrvadapter extends recyclerview.adapter<recyclerview.viewholder> { private final context mcontext; private arraylist<listmainitem> datahorizontal; private arraylist<listmainitem> datavertical; private static final int type_header = 0; private static final int type_item = 1; public maingridrvadapter(arraylist<listmainitem> datahorizontal, arraylist<listmainitem> datavertical, context context) { setdatahorizontal(datahorizontal); setdatavertical(datavertical); mcontext = context; } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { if (viewtype == type_item) { //inflate layout , pass view holder view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.row, parent, false); return new myviewholder(v); } else if (viewtype == type_header) { //inflate layout , pass view holder view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.row_header, parent, false); return new myviewholderheader(v); } throw new runtimeexception("there no type matches type " + viewtype + " + make sure using types correctly"); } @override public void onbindviewholder(recyclerview.viewholder holder, int position) { if (holder instanceof myviewholder) { string name = getdatavertical().get(position-1).getimages(); log.d("###", "setting name: " + name); ((myviewholder) holder).getmdataimgview().getresources().getidentifier(name,"drawable","com.example.pkg.pg.adapters"); // int resourceid = activity.getresources().getidentifier("testimage", "drawable", "your.package.name"); } else if (holder instanceof myviewholderheader) { //cast holder vhheader , set data header. log.d("####", "header"); } } @override public int getitemcount() { return datavertical.size() + 1; } @override public int getitemviewtype(int position) { if (ispositionheader(position)) { return type_header; } return type_item; } private boolean ispositionheader(int position) { return position == 0; } public void setdatahorizontal(arraylist<listmainitem> datahorizontal) { this.datahorizontal = datahorizontal; } public arraylist<listmainitem> getdatahorizontal() { return datahorizontal; } public void setdatavertical(arraylist<listmainitem> datavertical) { this.datavertical = datavertical; } public arraylist<listmainitem> getdatavertical() { return datavertical; } private class myviewholder extends recyclerview.viewholder { // private final textview mdatatextview; private final imageview mdataimgview; public myviewholder(view v) { super(v); // mdatatextview = (textview) v.findviewbyid(r.id.data_vertical); mdataimgview = (imageview) v.findviewbyid(r.id.img_vertical); } /* public textview getmdatatextview() { return mdatatextview; }*/ public imageview getmdataimgview() { return mdataimgview; } } private class myviewholderheader extends recyclerview.viewholder { private final recyclerview mhorizontalrecyclerview; public myviewholderheader(view v) { super(v); mhorizontalrecyclerview = (recyclerview) v.findviewbyid(r.id.recycler_view_horizontal); recyclerviewadapterhorizontal madapter = new recyclerviewadapterhorizontal(getdatahorizontal()); linearlayoutmanager layoutmanager = new linearlayoutmanager(mcontext, linearlayoutmanager.horizontal, false); mhorizontalrecyclerview.setlayoutmanager(layoutmanager); mhorizontalrecyclerview.setadapter(madapter); } } }
layout
<textview android:id="@+id/data_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="text" /> <imageview android:id="@+id/img_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" />
class recyclerviewadapterhorizontal extends recyclerview.adapter<recyclerview.viewholder> { private final arraylist<listmainitem> mdata; private final context mcontext; public recyclerviewadapterhorizontal(arraylist<listmainitem> datahorizontal,context context) { mdata = datahorizontal; mcontext=context; } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.row, parent, false); return new recyclerviewadapterhorizontal.myviewholderheader(v); } @override public void onbindviewholder(recyclerview.viewholder holder, int position) { string name = mdata.get(position).getimages(); log.d("###", "setting name: " + name); // ((recyclerviewadapterhorizontal.myviewholderheader) holder).getmdatatextview().settext(name); ((recyclerviewadapterhorizontal.myviewholderheader) holder).getmdataimgview().setimageresource(mcontext.getresources().getidentifier(name, "drawable", "com.example.mohamadmouazen.lebgame.adapters")); } @override public int getitemcount() { return mdata.size(); } private class myviewholderheader extends recyclerview.viewholder { // private final textview mdatatextview; private final imageview mdataimgview; public myviewholderheader(view v) { super(v); // mdatatextview = (textview) v.findviewbyid(r.id.data_vertical); mdataimgview = (imageview) v.findviewbyid(r.id.img_vertical); } public imageview getmdataimgview() { return mdataimgview; } /* public textview getmdatatextview() { return mdatatextview; }*/ }
edit
class recyclerviewadapterhorizontal extends recyclerview.adapter<recyclerview.viewholder> { private final arraylist<listmainitem> mdata; private final context mcontext; public recyclerviewadapterhorizontal(arraylist<listmainitem> datahorizontal,context context) { mdata = datahorizontal; mcontext=context; } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.row, parent, false); return new recyclerviewadapterhorizontal.myviewholderheader(v); } @override public void onbindviewholder(recyclerview.viewholder holder, int position) { string name = mdata.get(position).getimages(); log.d("###", "setting name: " + name); // ((recyclerviewadapterhorizontal.myviewholderheader) holder).getmdatatextview().settext(name); ((recyclerviewadapterhorizontal.myviewholderheader) holder).getmdataimgview().setimageresource(mcontext.getresources().getidentifier(name, "drawable", "com.example.pkg.pg.adapters")); } @override public int getitemcount() { return mdata.size(); } private class myviewholderheader extends recyclerview.viewholder { // private final textview mdatatextview; private final imageview mdataimgview; public myviewholderheader(view v) { super(v); // mdatatextview = (textview) v.findviewbyid(r.id.data_vertical); mdataimgview = (imageview) v.findviewbyid(r.id.img_vertical); } public imageview getmdataimgview() { return mdataimgview; } /* public textview getmdatatextview() { return mdatatextview; }*/ } }
change images
array int
s:
int[] images = { r.drawable.for_honor, r.drawable.ghost_recon, r.drawable.horizon_zerod, r.drawable.mass_effect };
and set image so:
int resourceid = images[position]; // <-- apply actual logic here ...getmdataimgview().setimageresource(resourceid);
Comments
Post a Comment