How to check if firebase recylerview has values or it is empty in android?

久未见 提交于 2019-12-02 10:57:28

You can use ValueEventListener to check if any values exists:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");
Query queries = ref.orderByChild("name").equalTo(name_here);
queries.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                Toast.makeText(HomeActivity.this,"data exists",Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(HomeActivity.this,"No data exists",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

In your Adapter, you need to use a query to be able to get the data, example as the above query and it will also check if the data exists.

Then you can configure the adapter by building FirebaseRecyclerOptions:

FirebaseRecyclerOptions<FriendsModel> friendsOptions =
        new FirebaseRecyclerOptions.Builder<FriendsModel>()
                .setQuery(queries, FriendsModel.class)
                .build();

Simply override onDataChanged and check for getItemCount() there.

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