android - Can't add items to recyclerView using arraylist -


i'm using firebase database app. can add , retrieve data database when i'm trying add retrieved data in arraylist can't populate arraylist.

 public void retrieveposts(){      retrieveposts.addvalueeventlistener(new valueeventlistener() {         @override         public void ondatachange(datasnapshot datasnapshot) {              toast toast = toast.maketext(getapplicationcontext(), "there " + datasnapshot.getchildrencount() + " blog posts", toast.length_long);             toast.show();              (datasnapshot postsnapshot : datasnapshot.getchildren()) {                 post post = postsnapshot.getvalue(post.class);                 //adding data list                   toast toastnew = toast.maketext(getapplicationcontext(), post.getauthor()+"_"+post.gettitle()+"_"+post.getcontent(), toast.length_short);                 toastnew.show();                 postlist.add(post);              }         }          @override         public void oncancelled(firebaseerror firebaseerror) {             snackbar snackbar = snackbar.make(coordinatorlayout, "unable fetch posts", snackbar.length_long);             snackbar.show();         }     });  } 

this retrieveposts method parsing datas, , postlist.add(post); line i'm trying add data arraylist. i've initialized arraylist before calling retrieveposts(); method , tried add arraylist in recyclyerview

 postlist = new arraylist<post>();     retrieveposts();     fab= (com.melnykov.fab.floatingactionbutton) findviewbyid(r.id.fab);     mrecyclerview = (recyclerview) findviewbyid(r.id.my_recycler_view);     mrecyclerview.sethasfixedsize(true);     mlayoutmanager = new linearlayoutmanager(this);     mrecyclerview.setlayoutmanager(mlayoutmanager);      madapter = new myrecyclerviewadapter(postlist);     mrecyclerview.setadapter(madapter); 

can't find i'm doing wrong. in advance!

if adapter has reference postlist, needs told when postlist changes, can tell recyclerview needs re-query (the adapter) updated views.

you can notifydatasetchanged() method on recyclerview.adapter.


you should wary - though it's unlikely, possible posts retrieved before adapter initialised (leading null pointer exception if use answer above). move retrieveposts() call last.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -