简体   繁体   中英

How do I get an Array(Number) from Firestore in Android

I am wanting to get an array(number) from firestore and then use that arrays contents(example[0], etc...). The reason for doing so is that im trying to use the contents to populate a line graph with it and though that this may be the best solution, however I am open to suggestions if you think there would be a better way. I have been unable to find a solution to this problem. Any help would be greatly appreciated.

If I'm getting you right then your problem can be resolved by the way below. fetch that content in ArrayList and run forloop.

in kotlin

for(number in arrayList){
//number is item for each time loop is execute till arrayList.size
}

in java

for(int i=0;i<arrayList.size,i++){
 arryList.get(i)
}

to get arrayList from firestore

  private val fireStore: FirebaseFirestore = FirebaseFirestore.getInstance()
    val arrayList:ArrayList<Users> =ArrayList()
            fireStore.collection("COLLECTION_NAME")
                    .get()
                    .addOnSuccessListener { snapshot->
                        for(document in snapshot){
                            val userData=document.toObject(Users::class.java)
                            userData.userId=document.id
                            arrayList.add(userData)
                        }
                    }
                    .addOnFailureListener { exception->
                        Toast.makeText(fragment.context, exception.message, Toast.LENGTH_SHORT).show()
                    }

User above is a model class you can made of your own kind. this code is in kotlin if you need java version then please visit the official documentation. link is below and do know me if you still got problems.

https://firebase.google.com/docs/firestore/query-data/get-data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM