Reputation: 323
I am getting data from Firebase
but RecyclerView
is not showing that data on first launch. It displays the data when I open the activity again.
In my main activity-
databaseReference = FirebaseDatabase.getInstance().getReference("data").child("childdata");
recyclerView = (RecyclerView)findViewById(R.id.recyclerviews);
recyclerView.setHasFixedSize(true);
recyclerAdapter = new DisplayRecyclerAdapter(list,this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(recyclerAdapter );
recyclerAdapter.notifyDataSetChanged();
loadRecyclerData();
}
private void loadRecyclerData() {
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
String da = postSnapshot.getValue().toString();
list.add(da);
}
});
}
Upvotes: 2
Views: 1006
Reputation: 815
Notify adapter inside onDataChange
cause your list is getting updated there
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot:dataSnapshot.getChildren()) {
String da = postSnapshot.getValue().toString();
list.add(da);
}
recyclerAdapter.notifyDataSetChanged();
}
)};
Upvotes: 4