Recylerview Not Automatically Loading Data From Firebase Android Studio
Solution 1:
The problem is with the retrieve
method: it's adding a ChildEventListener
that executes asynchronously (meaning it doesn't block until the children data are fetched) and then it immediately returns the spacecrafts
list which is still not yet filled by the asynchronous listener code.
You should refresh the RecyclerView
adapter in the actual code that fetches data, i.e. in the fetchData
method, e.g.:
privatevoidfetchData(DataSnapshot dataSnapshot)
{
spacecrafts.clear();
for (DataSnapshot ds : dataSnapshot.getChildren())
{
Spacecraft spacecraft=ds.getValue(Spacecraft.class);
spacecrafts.add(spacecraft);
}
adapter.spacecrafts = spacecrafts; // update the list in the adapter
adapter.notifyDataSetChanged(); // refresh
}
In this case, the retrieve
method can have a void
return type as it is pointless to return the list from it.
Solution 2:
Use FirebaseRecyclerAdapter for load the data from Firebase.
Solution 3:
modify the helper constructer to be like this:
public FirebaseHelper(Context c, DatabaseReference db, ListView lv) {
this.c = c;
this.db = db;
this.lv = lv;
}
then add this to the fetchData method after the for loop:
if (spacecrafts.size() > 0)
{
adapter = newCustomAdapter(c, spacecrafts);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
then in the main activity modify it like this:
helper = newFirebaseHelper(this, db, lv);
//ADAPTER//adapter = new CustomAdapter(this, helper.retrieve());//lv.setAdapter(adapter);
helper.retrieve();
this should work for you.
Solution 4:
Check if you are setting up adapter in onCreate() method or not, if you are calling it in onStart() then it will not load data automatically. Set Adapter in onCreate() method.
Post a Comment for "Recylerview Not Automatically Loading Data From Firebase Android Studio"