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

enter image description here

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
Ad