How do I handle Firebase Adapter RecyclerView empty view?

痞子三分冷 提交于 2021-02-07 10:23:21

问题


So the issue is that whenever I set my empty view for the recycler view, it always shows the empty view. I tried following some suggested solutions like the one in this url: FirebaseRecyclerAdapter with empty view, but the problem is that Firebase listeners are asynchronous and runs on its own thread. Before even setting the empty view, the fragment's onCreateView method already returns the view and so the empty view is not set appropriately. Are there any other way to implement the setting of empty view for FirebaseRecyclerAdapter?

Here is the snippet of the code:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.activity_list, container, false);

    initializeFilter();

    Log.d("AttendanceFragment", filter.getTab() + "");

    Firebase.setAndroidContext(getContext());

    //filter(filter);
    handleFilter();

    if(getContext() != null) {
        adapter = new AttendanceAdapter(Attendance.class, R.layout.list_item, AttendanceAdapter.AttendanceViewHolder.class, mDatabase);

        mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.d("DATA_CHANGED", dataSnapshot.hasChildren() + "");

                if(!dataSnapshot.hasChildren()){
                    Log.d("SETTING", setEmpty + "");
                    if(filter.getTab() == 1) {
                        empty = (TextView) v.findViewById(R.id.empty_view);
                        empty.setText("No finished attendance yet");
                    }

                    recView.setEmptyView(v.findViewById(R.id.empty_view));
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        Log.d("FIREBASE", recView.getAdapter().getItemCount() + "");
    }

    adapter.setOnItemClickListener(new AttendanceAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(String name) {
            Toast.makeText(getContext(), name, Toast.LENGTH_SHORT).show();
        }
    });

    recView.setAdapter(adapter);

    return v;
}

It basically checks if the dataSnapshot does not have any children and if it doesn't have any children, it sets the empty view. The problem again is that setAdapter() is called first before onDataChange().


回答1:


put the recView.setAdapter(adapter) after recView.setEmptyView(v.findViewById(R.id.empty_view)) for a better practice use ProgressBar or ProgressDialog



来源:https://stackoverflow.com/questions/43143134/how-do-i-handle-firebase-adapter-recyclerview-empty-view

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