Adding Object To Arraylist
I'm trying to add objects received from Firebase Database to an ArrayList. However, the list stays empty even after calling the add method. What am I doing wrong? I'm trying to loa
Solution 1:
You are getting null
because you are declaring the rescueList
ArrayList outside the onDataChange()
. This is happening due the asynchronous behaviour of this method. This means that the statement that adds those objects to the ArrayList is executed before onDataChange()
method has been called. To solve this you need to declare and use that ArrayList
inside onDataChange()
. Be careful not to add inside the for loop.
If you want to use those values outside that method, i sugget you readning my answer from this post.
Post a Comment for "Adding Object To Arraylist"