android - blank Recyclerview inside Fragment -
i using fragment , recyclerview together. have database , want query results coming database , display results inside activity.
however every time try run application , switch part , view results, don't seem anything. no results @ blank. don't know why it's not showing up.
this full code
sample class
public class sample extends fragment { private view rootview; public sample() {} @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { super.oncreateview(inflater, container, savedinstancestate); rootview = inflater.inflate(sample, container, false); recyclerview mrecyclerview = (recyclerview) rootview.findviewbyid(r.id.recyclerview_sample); mrecyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); mrecyclerview.sethasfixedsize(true); databasehandler db= new databasehandler(getactivity()); list<samplemodel> list = db.getresults(); sampleadapter sampleadapter = new sampleadapter(list); mrecyclerview.setadapter(sampleadapter); return rootview; } }
adapter class
public class sampleadapter extends recyclerview.adapter <sampleadapter.viewholder> { private list<samplemodel> list; private context mcontext; public sampleadapter (list<samplemodel> list) { list = list; } @override public sampleadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(mcontext).inflate(r.layout.sample_item, parent, false); return new viewholder(view); } @override public void onbindviewholder(sampleadapter.viewholder holder, int position) { samplemodel sample = list.get(holder.getadapterposition()); holder.title.settext(sample.gettitle()) } @override public int getitemcount() { return (list != null? list.size():0); } public class viewholder extends recyclerview.viewholder { public textview title; public viewholder(view itemview) { super(itemview); title = (textview) itemview.findviewbyid(r.id.title); } } }
xml
sample.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.coordinatorlayout 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" android:fitssystemwindows="true"> <include layout="@layout/sample_recyclerview" /> </android.support.design.widget.coordinatorlayout>
sample_recyclerview.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <android.support.v7.widget.recyclerview android:id="@+id/recyclerview_sample" android:layout_width="match_parent" android:layout_height="wrap_content" android:cliptopadding="false"/> </relativelayout>
sample_item.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.cardview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/cardview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="10dp" android:layout_marginbottom="10dp" android:layout_marginleft="16dp" android:layout_marginright="16dp" android:focusable="true" android:clickable="true" android:foreground="?android:attr/selectableitembackground" card_view:cardbackgroundcolor="#808080"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <textview android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#ffffff" /> </linearlayout> </android.support.v7.widget.cardview>
in sampleadapter
change
list = list;
to
this.list = list;
Comments
Post a Comment