java - Android Recyclerview not showing any data -
i took this tutorial reference. despite of checking everything, no data appearing on screen ie. main_activity. i've referred other pages on stackoverflow regarding issue. can't find viable solution.
note: there no warning or error while deploy app on device.
this source code.
mainactivity.java:
public class mainactivity extends activity { private recyclerview mrecyclerview; private recyclerview.adapter madapter; private recyclerview.layoutmanager mlayoutmanager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mrecyclerview = (recyclerview) findviewbyid(r.id.my_recycler_view); mrecyclerview.sethasfixedsize(true); mlayoutmanager = new linearlayoutmanager(this); mrecyclerview.setlayoutmanager(mlayoutmanager); madapter = new myrecyclerviewadapter(getdataset()); mrecyclerview.setadapter(madapter); recyclerview.itemdecoration itemdecoration = new divideritemdecoration(this, linearlayoutmanager.vertical); mrecyclerview.additemdecoration(itemdecoration); // code add item default animation dataobject obj = new dataobject("red", "foo"); ((myrecyclerviewadapter) madapter).additem(obj, 0); // code remove item default animation //((myrecyclerviewadapter) madapter).deleteitem(index); } @override protected void onresume() { super.onresume(); ((myrecyclerviewadapter) madapter).setonitemclicklistener(new myrecyclerviewadapter.myclicklistener() { @override public void onitemclick(int position, view v) { toast.maketext(mainactivity.this, "clicked item: " + position, toast.length_short).show(); } }); } private arraylist<dataobject> getdataset() { arraylist results = new arraylist<dataobject>(); (int index = 0; index < 20; index++){ dataobject obj = new dataobject("some primary text " + index, "secondary " + index); results.add(index, obj); } return results; } }
dataobject.java:
public class dataobject { private string mtext1; private string mtext2; dataobject (string text1, string text2){ mtext1 = text1; mtext2 = text2; } public string getmtext1() { return mtext1; } public void setmtext1(string mtext1) { this.mtext1 = mtext1; } public string getmtext2() { return mtext2; } public void setmtext2(string mtext2) { this.mtext2 = mtext2; } }
myrecyclerviewadapter.java:
public class myrecyclerviewadapter extends recyclerview.adapter<myrecyclerviewadapter.dataobjectholder> { private static string log_tag = "myrecyclerviewadapter"; private arraylist<dataobject> mdataset; private static myclicklistener myclicklistener; public static class dataobjectholder extends recyclerview.viewholder implements view.onclicklistener { textview label; textview datetime; public dataobjectholder(view itemview) { super(itemview); label = (textview) itemview.findviewbyid(r.id.textview); datetime = (textview) itemview.findviewbyid(r.id.textview2); log.i(log_tag, "adding listener"); itemview.setonclicklistener(this); } @override public void onclick(view v) { myclicklistener.onitemclick(getposition(), v); } } public void setonitemclicklistener(myclicklistener myclicklistener) { this.myclicklistener = myclicklistener; } public myrecyclerviewadapter(arraylist<dataobject> mydataset) { mdataset = mydataset; } @override public dataobjectholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.recyclerview_item, parent, false); dataobjectholder dataobjectholder = new dataobjectholder(view); return dataobjectholder; } @override public void onbindviewholder(dataobjectholder holder, int position) { holder.label.settext(mdataset.get(position).getmtext1()); holder.datetime.settext(mdataset.get(position).getmtext2()); } public void additem(dataobject dataobj, int index) { mdataset.add(dataobj); notifyiteminserted(index); } public void deleteitem(int index) { mdataset.remove(index); notifyitemremoved(index); } @override public int getitemcount() { return mdataset.size(); } public interface myclicklistener { public void onitemclick(int position, view v); } }
dividericondecoration.java
public class divideritemdecoration extends recyclerview.itemdecoration{ private static final int[] attrs = new int[]{ android.r.attr.listdivider }; public static final int horizontal_list = linearlayoutmanager.horizontal; public static final int vertical_list = linearlayoutmanager.vertical; private drawable mdivider; private int morientation; public divideritemdecoration(context context, int orientation) { final typedarray = context.obtainstyledattributes(attrs); mdivider = a.getdrawable(0); a.recycle(); setorientation(orientation); } public void setorientation(int orientation) { if (orientation != horizontal_list && orientation != vertical_list) { throw new illegalargumentexception("invalid orientation"); } morientation = orientation; } @override public void ondraw(canvas c, recyclerview parent) { if (morientation == vertical_list) { drawvertical(c, parent); } else { drawhorizontal(c, parent); } } public void drawvertical(canvas c, recyclerview parent) { final int left = parent.getpaddingleft(); final int right = parent.getwidth() - parent.getpaddingright(); final int childcount = parent.getchildcount(); (int = 0; < childcount; i++) { final view child = parent.getchildat(i); final recyclerview.layoutparams params = (recyclerview.layoutparams) child .getlayoutparams(); final int top = child.getbottom() + params.bottommargin; final int bottom = top + mdivider.getintrinsicheight(); mdivider.setbounds(left, top, right, bottom); mdivider.draw(c); } } public void drawhorizontal(canvas c, recyclerview parent) { final int top = parent.getpaddingtop(); final int bottom = parent.getheight() - parent.getpaddingbottom(); final int childcount = parent.getchildcount(); (int = 0; < childcount; i++) { final view child = parent.getchildat(i); final recyclerview.layoutparams params = (recyclerview.layoutparams) child .getlayoutparams(); final int left = child.getright() + params.rightmargin; final int right = left + mdivider.getintrinsicheight(); mdivider.setbounds(left, top, right, bottom); mdivider.draw(c); } } @override public void getitemoffsets(rect outrect, int itemposition, recyclerview parent) { if (morientation == vertical_list) { outrect.set(0, 0, 0, mdivider.getintrinsicheight()); } else { outrect.set(0, 0, mdivider.getintrinsicwidth(), 0); } } }
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativepackage}.${activityclass}" > <scrollview android:layout_width="match_parent" android:layout_height="match_parent"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.recyclerview android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" /> </relativelayout> </scrollview>
recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?android:attr/selectableitembackground" android:orientation="vertical"> <textview android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingtop="5dp" android:text="large text" android:textappearance="?android:attr/textappearancelarge" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingbottom="5dp" android:paddingleft="@dimen/activity_horizontal_margin" android:text="small text" android:textappearance="?android:attr/textappearancesmall" /> </linearlayout>
the problem in activity_main.xml. should remove scrollview , work (there's no reason have 1 since recyclerview has implicit one). there no scrollview in tutorial either: check activity_recycler_view.xml.
Comments
Post a Comment