1

enter image description hereFirebase screenshot

I would like to load data from firebase to an arraylist, but it does not seem to be working. Below are my codes and firebase screenshot. It's returning an empty arraylist. al2 is the arraylist:

void getstops(int IDs){
       String url = "https://bus-tracking-5ba3a.firebaseio.com/Route-Stops/";
        mDatabase2 = FirebaseDatabase.getInstance().getReferenceFromUrl(url);
        ValueEventListener vel = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot data : dataSnapshot.getChildren()){
                    al2.add(data.getValue().toString());
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };
        mDatabase2.child(Integer.toString(IDs)).addListenerForSingleValueEvent(vel);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
sj2
  • 19
  • 3
  • Unable to find firebase screenshot – Rishabh Saxena Apr 24 '19 at 12:15
  • Hi, the response https://stackoverflow.com/a/54415946/3468297 could be what you are looking for. – OliverTester Apr 24 '19 at 12:55
  • What are the actual values that you need to have in your ArrayList? – Alex Mamo Apr 24 '19 at 13:48
  • 1
    "It's returning an empty arraylist" Nothing in your code is *returning* an array list. Can you show how you determined that the array list is empty? Also keep in mind that Stack Overflow is a notoriously inefficient interactive debugger. So you might want to put a breakpoint on `for (DataSnapshot data ...`, see if that hits when you run in the debugger, and check what happens if you step through it. – Frank van Puffelen Apr 24 '19 at 13:51
  • I don't know if it is possible but when using the Log.d in the for loop, the array is not empty,, I get the values I need but when I used a Toast outside theValueEventListener to check whether the array is empty, it says true. I cannot figure this out – sj2 Apr 25 '19 at 16:27

0 Answers0