android - how to call RecyclerView in the first fragment of the navigation drawer? I've followed a tutorial by slidenerd but getting one error -
this java file of fragment wish insert recycler view. please dont worry package name deleted reasons.
import android.app.activity; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import java.util.arraylist; import java.util.list; public class dashboard extends fragment { private recyclerview recyclerview; private recycleradapter adapter; /** * returns new instance of fragment given section number. */ public static dashboard newinstance() { dashboard fragment = new dashboard(); return fragment; } public dashboard () { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.activity_dashboard, container, false); recyclerview = (recyclerview) rootview.findviewbyid(r.id.drawerlist); adapter=new recycleradapter(getactivity().getdata()); recyclerview.setadapter(adapter); recyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); return rootview; } public static list<information> getdata(){ list<information> data=new arraylist<>(); string[] titles={"rooms occupied","roomsvacant","check-in","check-out","extensions","confirmations","cancellations"}; for(int i=0;i<titles.length;i++) { information current =new information(); current.title=titles[i]; data.add(current); } return data; } @override public void onattach(activity activity) { super.onattach(activity); } }
this adapter class
import android.content.context; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; import java.util.collections; import java.util.list; /** * created gowtham on 6/13/2015. */ public class recycleradapter extends recyclerview.adapter<recycleradapter.myviewholder> { private layoutinflater inflater; list<information> data= collections.emptylist(); public recycleradapter(context context, list<information> data){ inflater=layoutinflater.from(context); this.data=data; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view=inflater.inflate(r.layout.custom_row, parent,false); myviewholder holder=new myviewholder(view); return holder; } @override public void onbindviewholder(myviewholder holder, int position) { information current=data.get(position); holder.title.settext(current.title); } @override public int getitemcount() { return 0; } class myviewholder extends recyclerview.viewholder{ textview title; public myviewholder(view itemview) { super(itemview); title= (textview) itemview.findviewbyid(r.id.viewtext); } } }
this layout file of fragment in want use recycler view
<?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"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/viewimage" android:background="@drawable/ic_dashboard" android:layout_gravity="center_vertical" android:gravity="center" /> <textview android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/viewtext" android:text="dummy text" android:textstyle="bold" android:layout_gravity="left" android:gravity="center" /> </linearlayout>
since i'm new stackoverflow don't have enough reputations add image. attach screenshot in comments below. please take @ know happening here exactly. thanks!
why call getactivity().getdata()
, call getdata()
.
and in adapter class:
@override public int getitemcount() { return data.size(); }
it can't return zero, should return data size.
Comments
Post a Comment