0

I would like to convert $document.data details to useful format so that I could use it for further applications. This is data from firestore documents.

private val mFireStore = FirebaseFirestore.getInstance()
            mFireStore.collection("Users").whereEqualTo("lastName","H").whereEqualTo("firstName", "Uc").get()
                .addOnSuccessListener{ documents ->
                    for(document in documents){
                        Log.d("TAG","${document.id}=>${document.data}")
                        Toast.makeText(applicationContext, "${document.id} => ${document.data}",
                                        Toast.LENGTH_LONG).show()


                    }
                }
                .addOnFailureListener{exception ->
                    Log.w("TAG","Error getting documents:",exception)
                    Toast.makeText(applicationContext, "Failed",
                        Toast.LENGTH_LONG).show()

                }

This is my code. Now when I run the code get this in the logcat

OL0rD4UfgHSh2K8UoTMnX6Xea6P2=>{lastName=H, image=, firstName=Uc, B=L, gender=, organization=, profileCompleted=0, mobile=0, blood group=o+, id=OL0rD4UfgHSh2K8UoTMnX6Xea6P2, email=jojoy09@gmail.com}

Now I want to convert this result to a useful format so that I could use it later. I wpuld like to convert the data so that I could load it in listview. enter image description here

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84

2 Answers2

0

The Model that you used to set this data will be used here. You can convert the documents to your Model class using the .toObjects() method on it. Just use:

val myObjs = documents.toObjects(Model::class.java)

EDIT

For displaying this as a Log in Logcat use:

Log.d("myObjs  ","""
    $myObjs 
""".trimIndent())

Do tell if this doesn't work for you :)

mehul bisht
  • 682
  • 5
  • 15
0

In the following for-loop:

for(document in documents) { ... }

The "document" object is of type DocumentSnapshot. When you call getData() on such an object, the type of object that is returned is a Map<String, Object>.

In Kotlin, this object is represented by Map<String, Any>. In order to get the data, you can simply iterate over the Map and get the data accordingly, using the following lines of code:

val map = document.data
for ((key, value) in map) {
    Log.d("TAG", "$key = $value")
}

Or even simpler, using:

map.forEach { (key, value) -> Log.d("TAG", "$key = $value") }

However, if you only need, the value of a particular property, for example, the value of the email address, you can simply get it by using DocumentSnapshot#getString(String field) method:

val email = document.getString("email")
Log.d("TAG", email)

The result in the logcat will be:

jojoy09@gmail.com
.................

As I see in your screenshot, almost all of the properties are of type String. However, you can find different flavors for each type of field, like getLong(String field), getDouble(String field), getDate(String field), getTimestamp(String field), and so on.

Furthermore, if you need to get the entire document, and you want to convert it into an object of a specific class, as also @mehulbisht mentioned in his answer, you should use DocumentSnapshot#toObject(Class valueType). So assuming that you have a data class that looks like this:

data class User(
    var email: String? = null,
    var firstName: String? = null,
    var lastName: String? = null,
    //Other necessary fields
)

To convert the "document" object into an object of the "User" class, please use the following line of code:

val user = document.toObject(User::class.java)
Log.d("TAG", user.email)

The result in the logcat will be the same as above.

If you want to display a list of "User" objects in a ListView, then please check my answer from the following post:

It's really simple to convert the Java code into Kotlin.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Regarding your issue, here you can find some info regarding the [no-argument constrcutore](https://stackoverflow.com/questions/66844341/could-not-deserialize-object-class-does-not-define-a-no-argument-constructor-if/66856453#66856453). – Alex Mamo Jul 18 '21 at 12:39