1

I have list of addresses in the Firebase Database. It is retrieved like this:

    addressesRef
        .addListenerForSingleValueEvent(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
                if (snapshot.exists()) {

                    
                    //Toast.makeText(this@AddressActivity, "Addresses fetched!", Toast.LENGTH_SHORT).show()
                }
            }

Notice the the addresses with 2 IDs (they got fetched in reverse order)

enter image description here

I am trying to convert this into an ArrayList<Address> where Address is my model class. How do I do that?

2
  • Do you need the value of a single child (Ad_100000) or of all children (Ad_100000, Ad_100001 etc)? Commented Aug 10, 2021 at 14:21
  • All of the children, hence the list. Commented Aug 10, 2021 at 18:06

1 Answer 1

1

It looks like what you have in the database is a Map<String, Address> and not a List<Address>. If you want to get just the Address value, you can either load the map and just get its value, or (preferred):

override fun onDataChange(snapshot: DataSnapshot) {
    if (snapshot.exists()) {
        for (addressSnapshot in snapshot.children) {
            val address = addressSnapshot.getValue<Address>();
            ...
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Where were you Sir?! I had to waste so many hours for this. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.