How can I apply loop to retrieve the required data from Firebase?

How can I apply loop to retrieve the required data from Firebase?



I am trying to get data from the following collection "Buses" if say Bus_Driver=="xyz".



My Firebase test looks like this:



enter image description here



I am getting database reference with this code:


busInfoReference= FirebaseDatabase.getInstance().getReference("Buses");



The following loop I've applied does not work correctly:


for(DataSnapshot ds : dataSnapshot.getChildren())
busInfo = ds.getValue(BusInforamtion.class);
if(ds.exists())
if(busInfo.getBus_Driver().equalsIgnoreCase(driverName))

saveData()

break;
//




How can I apply loop to get the required data? Thanks in advance.






then just check simple refrence.equalto(name) and trigger action.?

– Atif AbbAsi
Sep 16 '18 at 11:49




1 Answer
1



To solve this, you need query your database like this:


DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Buses").orderByChild("Bus_Driver").equalsTo("xyz");
ValueEventListener valueEventListener = new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
for(DataSnapshot ds : dataSnapshot.getChildren())
String Bus_Driver = ds.child("Bus_Driver").getValue(String.class);
Log.d(TAG, Bus_Driver);



@Override
public void onCancelled(@NonNull DatabaseError databaseError)
Log.d(TAG, databaseError.getMessage());

;
query.addListenerForSingleValueEvent(valueEventListener);



The output in your logcat will contain all bus driver names.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)