Ad
How To Iterate Through Children In Firebase - Java
I want to iterate through children in a Firebase real time-database node
this is my Database Structure.
this is my Code.
DatabaseReference RedLineRouteReference = FirebaseDatabase.getInstance().getReference().child("RedLineRoute");
RedLineRouteReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Iterator i = dataSnapshot.getChildren().iterator();
if(true) {
Toast.makeText(TimeTable.this, i.next().toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
This program outputs
I/TimeTable: DataSnapshot { key = aMaison, value = true }
However i would like to isolate each iteration(each child)
for instance I would like to define the second child and the Third Child and so on...
Ad
Answer
The way you deal with an Iterable in Java is with a simple for loop:
for (DataSnapshot child: dataSnapshot.getChildren()) {
String key = child.getKey();
String value = child.getValue().toString();
// do what you want with key and value
}
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad