0

I am trying to store all children of my firebase database into an array list. Here is my database structure:

enter image description here

I am currently trying to loop through children in order to get the values I need as follows:

private void initializeData(int Destination) {
    listItems = new ArrayList<>();

    DatabaseReference MyRef = FirebaseDatabase.getInstance().getReference("rideShare");

    switch (Destination) {
        case 0:
            Toast.makeText(getActivity(), "LA BUNDLE", Toast.LENGTH_SHORT).show();
            MyRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    // This method is called once with the initial value and again
                    // whenever data at this location is updated.
                    for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        String NameValue = snapshot.child("Name").getValue(String.class);
                        String Price = snapshot.child("Price").getValue(String.class);
                        String Type = snapshot.child("Trip Type").getValue(String.class);
                        String Date = snapshot.child("Date").getValue(String.class);
                        String Destination = "LA";
                        String Phone = snapshot.child("Phone Number").getValue(String.class);
                        listingItem newItem = new listingItem(NameValue, Type, Price, Date, Destination, Phone);
                        listItems.add(newItem);

                    }
                }

                @Override
                public void onCancelled(DatabaseError error) {
                    // Failed to read value
                    //Log.w(TAG , "Failed to read value.",error.toException());
                }
            });
            break;
    }
}
Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
Andrew
  • 1
  • 1

2 Answers2

0

Add all the string variables into a POJO class and name the variables same as the child nodes keys. Use snapshot.get(listingItem.class) directly instead. Refer to this https://stackoverflow.com/a/39861649/3860386

DarshanGowda0
  • 452
  • 3
  • 11
0

To get those values, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference rideShareRef = rootRef.child("rideShare");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<ListingItem> listItems = new ArrayList<>();

        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String nameValue = ds.child("Name").getValue(String.class);
            String type = ds.child("Trip Type").getValue(String.class);
            String price = ds.child("Price").getValue(String.class);
            String date = ds.child("Date").getValue(String.class);
            String destination = "LA";
            String phone = ds.child("Phone Number").getValue(String.class);
            ListingItem newItem = new ListingItem(nameValue, type, price, date, destination, phone);
            listItems.add(newItem);
        }
        Log.d("TAG", listItems);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
rideShareRef.addListenerForSingleValueEvent(eventListener);

When you are writting Java code, it's better to use Java Naming conventions. I added this code accordingly.

Also as you probaly see, i have added the declaration of listItems inside onDataChange() method, otherwise it will alwasy be null due the asynchronous behaviour of this method.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193