1

enter image description here

I have a Firebase database as shown in the pic. I am trying to retrieve data based on some condition but it shows no results.

    DbRef2 =FirebaseDatabase.getInstance().getReference().child("Notification");

    DbRef2.child("45961").orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()){
                Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_SHORT).show();
                Notification notf = dataSnapshot.getValue(Notification.class);
                Toast.makeText(getApplicationContext(),"hai"+notf.getFromId(),Toast.LENGTH_SHORT).show();
            }
        }

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

        }
    });

There is nothing shows in the toast. I also tried equalTo instead of startAt. but no results.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Vishal
  • 552
  • 1
  • 6
  • 20

2 Answers2

1

There are four issues in your code. The first one would be the query that you are using. If you want to get the results based on a condition, please change the following query:

DbRef2.child("45961").orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(/* ... */);

To:

DbRef2.orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(/* ... */);

There is no need for a .child("45961") call, as you need to loop through the results.

The second issue is that you get the DataSnapshot object as an argument but you don't loop to get the results. To actually get the results you should use the following code:

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    if (dataSnapshot.exists()){
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_SHORT).show();
            Notification notf = ds.getValue(Notification.class);
            Toast.makeText(getApplicationContext(),"hai"+notf.getFromId(),Toast.LENGTH_SHORT).show();
        }
    }
}

The third issue is the fact that you are not checking for potential error. You also might consider using:

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
    Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}

The fourth issue lies in the fact that you are using names that start with an uppercase. For that, I recommend you see my answer from the following post:

Firebase Android ListView not being displayed

Or:

how to read firestore sub-collection and pass it to FirestoreRecyclerOptions

If the fields in your class are lowercase.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

Change this:

 DbRef2.child("45961").orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(new ValueEventListener() {

into this:

DbRef2.orderByChild("FromId").startAt("22222").addListenerForSingleValueEvent(new ValueEventListener() {

If you want to use FromId inside orderByChild() then you don't need to access child("45961")

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • i tried it before it works but the result is null value. So i added that parent child(45961) – Vishal Sep 14 '20 at 09:09