Android Studio, RecyclerView crashes my app on start up -
i battling android recyclerviewer. followed tutorials , read posts here on stackoverflow, still not coming right.
mainactivity
public class mainactivity extends actionbaractivity { recyclerview myrecyclerview; myadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); myrecyclerview = (recyclerview)findviewbyid(r.id.myrecycleview); adapter = new myadapter(mainactivity.this, getdata()); final linearlayoutmanager layoutmanager = new linearlayoutmanager(mainactivity.this); layoutmanager.setorientation(linearlayoutmanager.vertical); myrecyclerview.setlayoutmanager(layoutmanager); myrecyclerview.setadapter(adapter); myrecyclerview.setitemanimator(new defaultitemanimator()); // } //the dataset public static list<mydatamodel> getdata() { list<mydatamodel> mydata = new arraylist<>(); string[] posttext = {"ibm", "apple", "google", "microsoft"}; int[] postimgurl = {r.drawable.google, r.drawable.ibm, r.drawable.microsoft, r.drawable.apple}; for(int x=0; x<posttext.length && x<postimgurl.length; x++) { mydatamodel mymodel = new mydatamodel(); mymodel.postimgurl = postimgurl[x]; mymodel.posttext = posttext[x]; mydata.add(mymodel); } return mydata; }
myadapter
public class myadapter extends recyclerview.adapter<myadapter.viewholder>{ list<mydatamodel> mydataset; private layoutinflater inflater; public myadapter(context cont, list<mydatamodel> mydataset) { inflater = layoutinflater.from(cont); } public myadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { //get itemlayoutview xml view itemlayoutview = inflater.inflate(r.layout.my_items_row, parent, false); //pass viewholder viewholder myviewholder = new viewholder(itemlayoutview); return myviewholder; } public void onbindviewholder(viewholder viewholder, int position) { viewholder.posttext.settext(mydataset.get(position).posttext); viewholder.postimage.setimageresource(mydataset.get(position).postimgurl); } public int getitemcount() { return mydataset.size(); } public static class viewholder extends recyclerview.viewholder { public textview posttext; public imageview postimage; public viewholder(view v) { super(v); posttext = (textview)v.findviewbyid(r.id.myposttext); postimage = (imageview)v.findviewbyid(r.id.mypostimage); } } }
my model data
public class mydatamodel { string posttext; int postimgurl; }
i testing on real device through android studio, other apps i testing fine. please thank you.
you didn't initialize mydataset
inside myadapter
class
Comments
Post a Comment