Rishab Khincha
Rishab Khincha

Reputation: 1

Unable to retrieve data from Firebase Realtime Database

I'm trying to display the "loc" of a part if its part number I given. Here is what the data structure looks like:

{
"parts":{
  "14521845": { "name":"TOOL EC160B/EC180B/EC210B/EC240", "loc":"EXC1", "sloc":"B3EGU01C03"},
  "12829050": { "name":"SWITCH; IGNITION SWITCH", "loc":"PS01", "sloc":"85-06-013"},
  "12829050": { "name":"SWITCH; IGNITION SWITCH", "loc":"COM1", "sloc":"B3RGK03D06"},
  "20044893": { "name":"PARTS CATALOG_ENG_SPA_FRE_GER_KOR_EC210D", "loc":"EXC1", "sloc":"B3EGT01B02"}
}
}

Activity Code:

FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
DatabaseReference databaseReference =firebaseDatabase.getReference("parts/"+curP);

            databaseReference.addChildEventListener(new ChildEventListener() {

                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                    Products data=dataSnapshot.getValue(Products.class);
                    Log.i("",String.valueOf(data.getLoc()));

                }

getLoc is the getter function for the Product class, and it returns the corresponding "loc" for the given curP. curP denoted the child values in parts.

The logic seems right to me, but I am not getting an output. Where am I going wrong here?

Upvotes: 0

Views: 679

Answers (3)

dantes_21
dantes_21

Reputation: 427

You can try to use ValueEventListener. If you want read data once so use the addListenerForSingleValueEvent method, something like this:

private void getFirebaseLocValue(int curP) {
    FirebaseDatabase firebase = FirebaseDatabase.getInstance();
    DatabaseReference mDatabase = firebase.getReference("parts");

    mDatabase.child(Integer.toString(curP))
        .addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.hasChildren()) {
                Products data = dataSnapshot.getValue(Products.class);
                Log.e("TAG", data.getLoc());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

Or you can use addValueEventListener and will get data with any changes. I really don't think that ChildEventListener is a good idea to retrieve data from Firebase.

Upvotes: 0

Lewis McGeary
Lewis McGeary

Reputation: 7922

The problem is that what you are getting in onChildAdded() is not a whole Product object as you expect it to be.

In your database reference you are targeting a specific Product ("parts/"+curP) but using a ChildEventListener. The children of a specific product node are name, loc and sloc, so the onChildAdded() will trigger several times, giving you each of these properties as a dataSnapshot separately.

The two patterns you might use to get whole Product objects are either:

  • add a ChildEventListener directly to the "parts" node and you will get each of the Products as a child of that node, or;
  • if you are adding a listener directly to the node of a particular product, use a ValueEventListener, to get the whole of that nodes entry as one dataSnapshot.

Upvotes: 0

Subhan Ali
Subhan Ali

Reputation: 1430

try this

getReference("parts").child(curP).addChildEventListener(new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                Products data = dataSnapshot.getValue(Products.class);
                Log.i("", String.valueOf(data.getLoc()));

            }
        });

Upvotes: 1

Related Questions