Unable to retrieve data from Firebase database


Vishal

enter image description here

I have a Firebase database as shown. I'm trying to retrieve data based on some criteria but it's not showing any 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 to show for the toast. I also tried equalTo instead of startAt. but no result.

Alex Momo:

There are four problems in your code. The first is the query you are using. If you want to get results based on conditions, change the following query:

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

to:

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

No need to .child("45961")call as you need to iterate over the results.

The second problem is that you are taking the DataSnapshotobject as a parameter, but not looping to get the result. To actually get the result 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 problem is that you are not checking for potential errors. You might also consider using:

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

The fourth problem is that you are using names that start with a capital letter. For this, I suggest you to check the answers from the following articles:

Firebase Android ListView not showing

either:

How to read Firestore subcollection and pass it to FirestoreRecyclerOptions

If the fields in your class are lowercase.

Related


Unable to retrieve data from Firebase database

Vishal I have a Firebase database as shown. I'm trying to retrieve data based on some criteria but it's not showing any results. DbRef2 =FirebaseDatabase.getInstance().getReference().child("Notification"); DbRef2.child("45961").orderByChild("FromId").

Unable to retrieve data from Firebase database

Naga Hermans Here is my component. I don't know why the data cannot be retrieved from Firebase. Can anyone tell me what's wrong in this code? import { Component, OnInit, Input, ViewChild } from '@angular/core'; import { AngularFireDatabase, FirebaseListObserva

Unable to retrieve data from Firebase Realtime Database

Manjujohn I am a beginner in angular. When I try to retrieve the data using the AngularFireDatabase object, I don't get the data in the required format interface: export interface AppUser { name: string; email: string; isAdmin: boolean; } function call: get

Unable to retrieve data from Firebase database

Naga Hermans Here is my component. I don't know why the data cannot be retrieved from Firebase. Can anyone tell me what's wrong in this code? import { Component, OnInit, Input, ViewChild } from '@angular/core'; import { AngularFireDatabase, FirebaseListObserva

Unable to retrieve data from Firebase Realtime Database

Manjujohn I am a beginner in angular. When I try to retrieve the data using the AngularFireDatabase object, I don't get the data in the required format interface: export interface AppUser { name: string; email: string; isAdmin: boolean; } function call: get

Unable to retrieve OrderbyChild() data from Firebase database

chinna 98 My database structure is shown below: I used to use orderByChild()Input input to retrieve data from Firebase database RegId, but I get the value as NULL. Please tell me how to retrieve data using RegIduser as input . return admin.database().ref('Tabl

Unable to retrieve data from Firebase database

Vishal I have a Firebase database as shown. I'm trying to retrieve data based on some criteria but it's not showing any results. DbRef2 =FirebaseDatabase.getInstance().getReference().child("Notification"); DbRef2.child("45961").orderByChild("FromId").

Unable to retrieve data from Firebase database

Vishal I have a Firebase database as shown. I'm trying to retrieve data based on some criteria but it's not showing any results. DbRef2 =FirebaseDatabase.getInstance().getReference().child("Notification"); DbRef2.child("45961").orderByChild("FromId").

Unable to retrieve data from Firebase database

Naga Hermans Here is my component. I don't know why the data cannot be retrieved from Firebase. Can anyone tell me what's wrong in this code? import { Component, OnInit, Input, ViewChild } from '@angular/core'; import { AngularFireDatabase, FirebaseListObserva

Unable to retrieve OrderbyChild() data from Firebase database

chinna 98 My database structure is shown below: I used to use orderByChild()Input input to retrieve data from Firebase database RegId, but I get the value as NULL. Please tell me how to retrieve data using RegIduser as input . return admin.database().ref('Tabl

Unable to retrieve data from JSON in Firebase Database Swift

Shirley Charling Lee I risk using Firebase for my final project. I am unable to retrieve data from Firebase database. I don't know where I am going wrong. Please enlighten me. Main file: var ref: FIRDatabaseReference! let user = FIRAuth.auth()?.currentUser @I

Unable to retrieve data from database

ALİak I am trying to connect mysql db and get some data. But the echo command does nothing. What am I doing wrong again? Why am I getting undefined index: konu_baslik error <?php try{ $db=new PDO("mysql:host=localhost;dbname=blog","root",""); } catch (PD

Firebase retrieve data from database

Danilo Lamazales Hi I am trying to retrieve a list from the database and add it to a ListView, my problem is that the event fires only after onResume() so the array on the onCreate() function is empty. I want to retrieve data on onCreate() function and create

Firebase Database - Retrieve data from database

Zapi Mance I'm new firebase developer and working on android application which uses firebase database. This is part of my database. Here is my model class: public class Exam { private String id; private int subject; private String school; priva

Unable to retrieve output data from database

cross I am using the following PHP code to retrieve the store information field from the database, but when I run the PHP I am getting an error as shown below. <?php include ('database.php'); $locLat ; $locLng ; $shopName; $shopContact; $sql = "SELECT s_iD,

Django - Unable to retrieve data from database in dropdown

Gerry Hi I really need help, tried many days and still can't. I am new to Django. Problem: I can't retrieve the data in the MySQL database to display it in the dropdown list. Can someone help me? Grateful! models.py class paper(models.Model): id = models.CharF

Unable to retrieve data for column from database

Azmat Inayat I have a table in the database called keywords. I want to retrieve the data for the alert and location columns from this table, but I can't retrieve them except for the contact number. Now I am showing its value in the Toast but everytime I run an

Django - Unable to retrieve data from database in dropdown

Gerry Hi I really need help, tried many days and still can't. I am new to Django. Problem: I can't retrieve the data in the MySQL database to display it in the dropdown list. Can someone help me? Very grateful! models.py class paper(models.Model): id = models.

Unable to retrieve data for column from database

Azmat Inayat I have a table in the database called keywords. I want to retrieve the data for the alert and location columns from this table, but I can't retrieve them except for the contact number. Now I am showing its value in the Toast but everytime I run an

Unable to retrieve data for column from database

Azmat Inayat I have a table in the database called keywords. I want to retrieve the data for the alert and location columns from this table, but I can't retrieve them except for the contact number. Now I am showing its value in the Toast but everytime I run an

Unable to retrieve data from Firebase in Django

Ansel D'souza: I've been trying to get data from Firebase into my Django app and the problem I'm facing is that some documents are being retrieved and some are not. One very strange thing I've noticed is that on the admin page, the documents that are accessibl

Unable to retrieve data from Firebase in Django

Ansel D'souza: I've been trying to get data from Firebase into my Django app and the problem I'm facing is that some documents are being retrieved and some are not. One very strange thing I've noticed is that on the admin page, the documents that are accessibl

Unable to retrieve data from Firebase using dataSnapshot

Kartik Singh | My Firebase database looks like above I'm trying to retrieve the 'a' value of 'alind' and similarly I'm using Android Studio to retrieve another child node 'kartik'. but I am getting this error - java.lang.NullPointerException: Attempt to invoke

Unable to retrieve data from Firebase by retrieving 'number'

build new I am new to learning how to create an app using android studio and firebase realtime storage. I'm trying to make an app that can easily retrieve details from firebase, the apps stop by themselves when trying to retrieve details from firebase. As laid