AkashK
AkashK

Reputation: 323

RecyclerView does not load data from Firebase on first launch

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

Answers (1)

Shruti
Shruti

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

Related Questions