简体   繁体   中英

Data not retrieving from Firebase database

在此处输入图片说明

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.

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.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM