Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Data from Firebase realtime database, Android Studio, Java

I am looking for a way to get data from the firebase real-time database in a format like an array[] or a String.

My database looks like this: Link to image of database

Or: Database | -->Users | -->UID1 -->UID2

This is at the root of the database I want to get a list of all of the UIDs in the "Users" child. This is the code I have so far and am a bit stuck on:

DatabaseReference databaseReference = firebaseDatabase.getReference("Users");
        databaseReference.addValueEventListener(
                new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String UIDs = dataSnapshot.getValue(String.class);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });

I am a bit of a rookie as it comes to java, android studio, and firebase. I am trying to get the data in a format I know how to use like a String or an Array[] of Strings. I looked around if other people had maybe asked the same question, but I could get the answers to those questions to work/didn't understand them. Thanks in advance for your time!

like image 868
Jochem Sallander Avatar asked Jan 27 '23 21:01

Jochem Sallander


2 Answers

To get the a list of uids, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String uid = ds.getKey();
            list.add(uid);
        }

        //Do what you need to do with your list
    }

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

I recommend you to use the list only inside the callback otherwise it will be empty. If you want to use it outside the onDataChange() method, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

like image 158
Alex Mamo Avatar answered Jan 31 '23 18:01

Alex Mamo


For your above question I will give both ways: ArrayList or String with delimiters

ArrayList<String> uids = new ArrayList<String>();

FirebaseDatabase.getInstance().getReference("Users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    if (dataSnapshot.exists()) {
        for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
            uids.add(snapshot.getKey());
        }
    }
}

@Override
public void onCancelled(DatabaseError error) {

}
});

For String

String uids = "";

FirebaseDatabase.getInstance().getReference("Users").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
                uids += snapshot.getKey() + ",";
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError error) {

    }
    });

This gives an output such as: uid1,uid2,uid3,.....uidn,

like image 40
Prodigy Avatar answered Jan 31 '23 19:01

Prodigy