Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase/android how get children keys and values?

I have this DB:

users: {
     0 : {
         name: xx
         age: 11
         books:{
               0 : true
               3 : true
         }
     }
     1 : {
         name: yy
         age: 12
         books:{
               1 : true
               4 : true
         }
     }
}

I have ref to db als:

database = FirebaseDatabase.getInstance();
refdb = database.getReference();

and query als:

Query = refdb.child("users");

i need to get all pairs (name:value) and for users pairs of books (id:true) als:

0 : true
3 : true

I'm using this to get pairs (id:true):

refdb.child("users").child("books").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) { 
        pck.setText(pack);
        for (DataSnapshot dttSnapshot2 : dataSnapshot.getChildren()) {
             pack = dttSnapshot2 .getKey().toString()+":"+dttSnapshot2 .getValue().toString();
             pck.append(pack);
         }
...

Where pck is a textView. My problem is that everytime he print the empty string in textView. How can I get those data and write them in a textview?

like image 290
mixx Avatar asked Nov 03 '17 14:11

mixx


People also ask

How do I get a key in firebase?

Firebase automatically creates API keys for your project when you do any of the following: Create a Firebase project > Browser key auto-created. Create a Firebase Apple App > iOS key auto-created. Create a Firebase Android App > Android key auto-created.

How do I get DataSnapshot data?

A DataSnapshot is passed to the event callbacks you attach with on() or once() . You can extract the contents of the snapshot as a JavaScript object by calling the val() method. Alternatively, you can traverse into the snapshot by calling child() to return child snapshots (which you could then call val() on).

How do I get information from Firebase?

Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events.


1 Answers

refdb.child("users").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for(DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()){
            //Loop 1 to go through all the child nodes of users
            for(DataSnapshot booksSnapshot : uniqueKey.child("Books").getChildren()){
            //loop 2 to go through all the child nodes of books node
                String bookskey = booksSnapshot.getKey();
                String booksValue = booksSnapshot.getValue();
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
}

I haven't tested this code as of now, but I hope you get the idea. The unique ID nodes cannot be skipped because firebase does not know which unique id you want to extract data from. So either go through all the unique IDs by using a foreach loop or specify the unique ID from which you want the data.

like image 79
vivek verma Avatar answered Oct 13 '22 13:10

vivek verma