Ad
Firebase - Add Children To Array List - Java
I have a list of Estates in a route that a bus visits, however somedays the estates close and an Admin has to change the Route List, I would like the children in the RedLineRoutes to be changeable, I want to iterate through a node and get all the children and insert them into an Array, is that possible
My Database
My Code
void mConditions()
{
Log.d(TAG,"STARTED mCONDITIONS");
DatabaseReference RedLineRouteReference = FirebaseDatabase.getInstance().getReference().child("RedLineRoute");
RedLineRouteReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
String key = child.getKey();
String value = child.getValue().toString();
Log.i(TAG,key+"\n"+value);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Here is the output
I/TimeTable: aMaison
true
I/TimeTable: bMontRochelle
true
cHoldenManz
true'
I/TimeTable: dCharmonix
true
I/TimeTable: eDieuDonne
true
I/TimeTable: fGrandeProvance
true
gRicketyBridge
true
aMaison
true
I/TimeTable: bMontRochelle
true
cHoldenManz
true'
dCharmonix
true
eDieuDonne
true
fGrandeProvance
true
I/TimeTable: gRicketyBridge
true
aMaison
true
bMontRochelle
true
cHoldenManz
true'
dCharmonix
true
eDieuDonne
true
I/TimeTable: fGrandeProvance
true
gRicketyBridge
true
how would I get the result of iterating through the children into an Array that I can use?
Ad
Answer
I have found an Answer thanks to this question
but here is the code, if you want to iterate through a list of children in a firebase databse, and isolate each child of the node.
DatabaseReference RedLineRouteReference = FirebaseDatabase.getInstance().getReference().child("RedLineRoute");
RedLineRouteReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ArrayList<String> youNameArray = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String data = snapshot.getKey();
youNameArray.add(data);
}
Log.v(TAG, "First data : " + youNameArray.get(0));
Log.v(TAG, "Second data : " + youNameArray.get(1));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
and here is the output
V/TimeTable: First data : aMaison
Second data : bMontRochelle
V/TimeTable: First data : aMaison
Second data : bMontRochelle
So I am able to access each child and isolate each one and call them with their position in the Array.
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