android - RecyclerView Adapter (firebase) with cardview not showing -


i tried adapt simple adapter populate recycler cardview. began saving firebase data arraylist transferred dummy class (to hold variable). when run app said

attempt invoke virtual method 'int java.util.arraylist.size()' on null object reference...at  arrayrecycleradapter.getitemcount(arrayrecycleradapter.java:26) 

i tested make sure saving/retrieving globaldata correctly adding line

test = ((globaldata) getactivity().getapplication()).getmachinedata(); 

so figured wrong adapter dont know is. thanks

card.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" >  <android.support.v7.widget.cardview     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:id="@+id/cv"     >      <relativelayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:padding="16dp"         >          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/machine_name"             android:layout_alignparentleft="true"             android:layout_alignparenttop="true"             android:layout_marginright="16dp"             android:textsize="30sp"             />          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:id="@+id/machine_info"             android:layout_below="@+id/machine_name"             />      </relativelayout>  </android.support.v7.widget.cardview>  </linearlayout> 

main .xml

<linearlayout 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" android:orientation="vertical">  <textview     android:id="@+id/machinie_title"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_margin="16dp"     android:text="@string/bar_machine"     android:textalignment="center"     android:textsize="50sp" />  <relativelayout     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp">      <android.support.v7.widget.recyclerview         android:id="@+id/recycler_view"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:scrollbars="vertical"/> </relativelayout>    </linearlayout> 

adapter .java

public class arrayrecycleradapter extends  recyclerview.adapter<arrayrecycleradapter.mviewholder> {  private arraylist<machineclass> machineclasslist;  public arrayrecycleradapter(arraylist<machineclass> mlist) {     this.machineclasslist = mlist; }   @override public int getitemcount() {     return machineclasslist.size(); }  @override public void onbindviewholder(mviewholder mviewholder, int i) {     machineclass ci = machineclasslist.get(i);     mviewholder.vname.settext(ci.getmname());     mviewholder.vsurname.settext(ci.getmstatus());  }  @override public mviewholder oncreateviewholder(viewgroup viewgroup, int i) {     view itemview = layoutinflater.             from(viewgroup.getcontext()).             inflate(r.layout.machine_card, viewgroup, false);      return new mviewholder(itemview); }  public static class mviewholder extends recyclerview.viewholder {      protected textview vname;     protected textview vsurname;       public mviewholder(view v) {         super(v);         vname =  (textview) v.findviewbyid(r.id.machine_name);         vsurname = (textview)  v.findviewbyid(r.id.machine_info);      } } } 

main.java

public class machinepage extends fragment {  public machinepage() {     // required empty public constructor }   public static machinepage newinstance() {     machinepage fragment = new machinepage();      return fragment; }  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);  }  @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout fragment     view rootview = inflater.inflate(r.layout.fragment_machine_page, container, false);     recyclerview recyclerview = (recyclerview)rootview.findviewbyid(r.id.recycler_view);     recyclerview.sethasfixedsize(true);     linearlayoutmanager llm = new linearlayoutmanager(getcontext());     recyclerview.setlayoutmanager(llm);      firebasedatabase database = firebasedatabase.getinstance();     final databasereference myref = database.getreference("resources/machines");       final arraylist<machineclass> machinelist = new arraylist<>();     arraylist<machineclass> test;      myref.addvalueeventlistener(new valueeventlistener() {         @override         public void ondatachange(datasnapshot datasnapshot) {             for(datasnapshot mchlist: datasnapshot.getchildren()){                 machineclass mach = mchlist.getvalue(machineclass.class);                 machinelist.add(mach);             }              ((globaldata) getactivity().getapplication()).setmachinedata(machinelist);         }          @override         public void oncancelled(databaseerror databaseerror) {          }     });      test = ((globaldata) getactivity().getapplication()).getmachinedata();      arrayrecycleradapter adapter = new arrayrecycleradapter(test);     recyclerview.setadapter(adapter);          return rootview; }  } 

and globaldata.java

public class globaldata extends application { //saves user id globally + user specific path private string userid; private tag usertag; private string userpath; private arraylist<machineclass> machinedata;  public string getuserid() {     userid = tohexstring(usertag.getid());     return userid; } public string getuserpath(){     userpath = "users/" + userid + "/";     return userpath; }  public void setusertag(tag tag) {     this.usertag = tag; }  public static string tohexstring(byte[] bytes) {     char[] hexarray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};     char[] hexchars = new char[bytes.length * 2];     int v;     (int j = 0; j < bytes.length; j++) {         v = bytes[j] & 0xff;         hexchars[j * 2] = hexarray[v / 16];         hexchars[j * 2 + 1] = hexarray[v % 16];     }     return new string(hexchars); } //end of save user id   public arraylist<machineclass> getmachinedata() {     return machinedata; }  public arraylist setmachinedata(arraylist<machineclass> machinedata) {    return machinedata = machinedata; } } 

try way. initialize test first like

list<machineclass> test = new arraylist<>(); 

then, when assigning value on test.

test.addall(((globaldata) getactivity().getapplication()).getmachinedata()); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -