OliverDamon
OliverDamon

Reputation: 147

Create exception in firebase

I am trying to filter a listing in firebase. I am using equals to fetch a value that is inside, and now I want to make an excess if there is no data with that value.

for (DataSnapshot data : dataSnapshot.getChildren()) {
 if (data.child("statusBookmark").getValue().equals("1")) {
    final Profile profile2 = data.getValue(Profile.class);
    profile.add(profile2);
  }
}

Upvotes: 0

Views: 62

Answers (2)

PradyumanDixit
PradyumanDixit

Reputation: 2375

This can also be done easily by using orderByChild() as follows:

reference.child("parent").orderByChild("statusBookmark").equalTo("1").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                    final Profile profile2 = data.getValue(Profile.class);
                    profile.add(profile2);

                }
                else
                   Log.d("readThis", "no data available for corresponding value"); 

            }

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

            }
        });

Upvotes: 1

olajide
olajide

Reputation: 967

You can just simply do the following:

    for (DataSnapshot data : dataSnapshot.getChildren()) {

    if(data.exists()){
    if (data.child("statusBookmark").getValue().equals("1")) {
        final Profile profile2 = data.getValue(Profile.class);
        profile.add(profile2);
      }else{
       log.d("check", "Data is not equals to 1");
    }

  } else{
     log.d("check", "Data does not exist");
    }   
  }

Upvotes: 0

Related Questions