Skip to content
Advertisement

How to check recyclerview is empty or not using dataSnapshot.hasChildren()

I am using firebasedatabase to show my data into recyclerview. I want to show simple toast when my recyclerview is empty and i am using dataSnapshot.hasChildren() for checking the database haschildren or not. here is code

  private void Geofireinit() {
    started = true;
    databaseReference=FirebaseDatabase.getInstance().getReference().child(("tracker"));
    GeoFire fire=new GeoFire(databaseReference);
    GeoQuery geoQuery = fire.queryAtLocation(new GeoLocation(20.887715, 77.757623), 50);
    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {


            Log.e("id", key);


            getkeys(key);


        }

        @Override
        public void onKeyExited(String key) {
            Log.d("TAG", "onKeyExited: ");




            for (int i=0;i<arrayList.size();i++)
            {
               Log.i("tte",String.valueOf(arrayList.get(i).getText()));
               if (key.equals(arrayList.get(i).getText())) {
                   arrayList.remove(i);
                   adapter.notifyItemRemoved(i);

               }
               if (arrayList.size()==0)
               {
                   Log.i("exited",String.valueOf(arrayList.size()));
               }

            }


        }

        @Override
        public void onKeyMoved(String key, GeoLocation location) {

        }

        @Override
        public void onGeoQueryReady() {


        }

        @Override
        public void onGeoQueryError(DatabaseError error) {

        }
    });

}

ArrayList<Mylist> arrayList=new ArrayList<>();
private void getkeys(String key)
{
    myref=FirebaseDatabase.getInstance().getReference().child("buses").child(key);
    myref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists())
            {

                Mylist mylist=new Mylist();
                mylist.setText(dataSnapshot.child("text").getValue().toString());
                arrayList.add(mylist);
                adapter=new Adapter(MainActivity.this,arrayList);
                recyclerView.setAdapter(adapter);
                Log.i("chaadad", String.valueOf(dataSnapshot.hasChild("buses")));
            }

            if (dataSnapshot.hasChildren())
            {
               // do something
            }else
              Toast.makeText(MainActivity.this,"empty",Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

when data is available the dataSnapshot.hasChildren() giving true value but when no data available else condition is not working

Advertisement

Answer

you can use dataSnapshot.getChildrenCount()

if (dataSnapshot.getChildrenCount() > 0)
{
    // do something
} else
    Toast.makeText(MainActivity.this,"empty",Toast.LENGTH_SHORT).show();

Update:

check the else condition for the .exists() for empty list

if (dataSnapshot.exists())
{

    Mylist mylist=new Mylist();
    mylist.setText(dataSnapshot.child("text").getValue().toString());
    arrayList.add(mylist);
    adapter=new Adapter(MainActivity.this,arrayList);
    recyclerView.setAdapter(adapter);
    Log.i("chaadad", String.valueOf(dataSnapshot.hasChild("buses")));

} else
    Toast.makeText(MainActivity.this,"empty",Toast.LENGTH_SHORT).show();
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement