Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve specific value from Firebase database into string variable

I'm trying to retrieve a specific value from a Firebase realtime Database.

The data looks like this:

 The data looks like this;

I want to get the URL value from any of them and then put it into a string variable to use later. This is so that the program does not need to be changed if the download link is changed. The code I have so far is;

DatabaseReference fdb = FirebaseDatabase.getInstance().getReference();
//Query query = fdb.child("URLDOWNLOADS").child("WSSeasons")
fdb.addListenerForSingleValueEvent(new ValueEventListener()
{
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
    {
        for (DataSnapshot url : dataSnapshot.getChildren())
        {
            LoginInfoFirebasedb DownURL = url.getValue(LoginInfoFirebasedb.class);
            String temp = DownURL.dwldURL;
            URL1 = temp;
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError)
    {

    }
});

Some help would be seriously appreciated. I understand this may be a simple problem but I am stumped.

like image 420
Alexander Mcmichael Avatar asked Aug 13 '26 07:08

Alexander Mcmichael


1 Answers

To get each url as a String, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("URLDOWNLOADS");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String key = ds.getKey();
            String url = ds.child("URL").getValue(String.class);
            Log.d(TAG, key + " / " + url);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
ref.addListenerForSingleValueEvent(valueEventListener);
like image 86
Alex Mamo Avatar answered Aug 14 '26 23:08

Alex Mamo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!