RecyclerView does not load data from Firebase on first launch

房东的猫 提交于 2019-12-06 11:14:17

问题


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);
        }
    });
}

回答1:


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(); 
    }
)};


来源:https://stackoverflow.com/questions/46926421/recyclerview-does-not-load-data-from-firebase-on-first-launch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!