1

i try to get random child from my firebase. but i cant, i was try from many idea from stackoverflow too but still not working. for example, i want get random child from "Bahan Makanan Susu", and after get a random child i want show the value of "kalori".

This my database created

enter image description here

this my currently code

reff = FirebaseDatabase.getInstance().getReference();
reff.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull  DataSnapshot dataSnapshot) {
        String Sskal =dataSnapshot.child("Bahan Makanan Susu").child("3").child("Kalori").getValue().toString();

'child("3")' in here i create manually and i want to make it random. thankyou hope u all can help me

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
lazycode
  • 11
  • 1

2 Answers2

0

Try this:

reff = FirebaseDatabase.getInstance().getReference("Bahan Makanan Susu/3/Kalori");
reff.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull  DataSnapshot dataSnapshot) {
        String Sskal = dataSnapshot.getValue().toString();
        Log.i("Firebase", Sskal);
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) { 
        throw databaseError.toException(); 
    }
}

The main changes and points to pay attention to:

  • This code reads only property from the database, passing in the entire path to that node/property in to getReference.
  • Always implement onCancelled as it can point out access problems for your database. If you get an exception for this, search for the error message to learn how to deal with it.
  • If this also doesn't read any value, you've never been able to read or write to the database, and your database is in another region than the US, consider re-downloading your google-services.json file, or specifying the database URL in the getInstance("URL here") call in your code.
  • You really shouldn't use sequential numeric keys like this. They're an anti-pattern in Firebase, as the SDKs try to interpret them as arrays. For more on this, see Best Practices: Arrays in Firebase.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thankyou so much for your answer :). but in here i try to get random child from "Bahan Makanan Susu" like "1", "2" or "3" randomly, as you can see from my image structure database, and after that i want get the value from "Kalori" – lazycode Jan 07 '22 at 05:38
  • I have no idea what you mean there @lazycode. Did you try my code? Did it give any errors? What did it log? – Frank van Puffelen Jan 07 '22 at 16:24
  • I guess he is trying to say that he wants to get random node inside `Bahan Makan Susu` – Sambhav Khandelwal Jan 07 '22 at 16:30
0

Try this:

reff = FirebaseDatabase.getInstance().getReference();
reff.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull  DataSnapshot 
dataSnapshot) {
        Random r = new Random();
        int total = reff.child("Bahan Makanan Susu").getChildrenCount();
        String Sskal =dataSnapshot.child("Bahan Makanan 

Susu").child(r.nextInt(total)).child("Kalori").getValue().toString(); });

You would have to do a check everytime though so one child doesn't get repeated but I hope you will be able to make it work.

Sujal Kumar
  • 1,054
  • 10
  • 23