0

Here is the code

val db = Firebase.firestore

val user = hashMapOf(
    "name" = "{binding.edit_name.text.toString()}",
    "email" = "{binding.edit_email.text.toString()}
)
binding.submitBtn.setOnClickListener{
    db.collection("users").add(user)
        .addOnSuccessListener {
            Toast.makeText(context,"Data inserted",Toast.LENGTH_LONG).show()
        }
        .addOnFailureListener {
            Toast.makeText(context,"Error",Toast.LENGTH_LONG).show()
        }
}

In above code edit_name , edit_text is input taken by keyboard. I can't able to store the user in firestore. I think there is problem of converting bindig.edit_name.text.toString() it can't able to convert string. If i use hash map without taking input from keyboard as below then I am able to insert data in firestore.

val user = hashMapOf(
    "name" to "ABC",
    "email" to "abc@gmail.com"
)

This hash map is able to add in firestore.

I think there is problem in binding and I am also can't able to toast bindg.edit_name.toString() as shown below

binding.submitBtn.setOnClickListener{
    Toast.makeText(context,"${binding.edit_name.text.toString()},Toast.LENGTH_LONG).show()
}

Please help in this. I think all problem is that I can't able to convert binding data value as a string (by default it is Editable).

2 Answers2

1

According to the official documentation your code seems fine from the point of view of inserting data.

The issue seems indeed to rely on "binding.variable_input.text.toString()".

While investigating the Android Data Binding Library I came across this post, which recommends instead of using toString(), use:

  • String.valueOf(), or if you want to use toString(), try

  • Integer.valueOf().toString()

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    Your suggested link and answer for xml not for fragment. I want to convert binding data in kotlin fragment file then I want to add in database. Without converting in string it is not able to insert in db because type of binding data is Editable. – Vikas Kumar Oct 07 '20 at 04:22
  • Could you please elaborate further on how you are storing the text input in binding? – Artemis Georgakopoulou Oct 07 '20 at 13:11
  • I have taken input from keyboard then I stored in variable using binding `val name = binding.name.text.toString()` but due to this I can't able store in db... If you see above my ques with code my question will be cleared – Vikas Kumar Oct 08 '20 at 07:00
  • If I am using binding in my fragment then if we take input from keyboard then that input will be visible in **Kotlin file** by binding.text.toString() but it is not able to add in db – Vikas Kumar Oct 08 '20 at 07:02
1
  1. Expressions cannot be used for assignments in HashMap, so probably worth changing hashMapOf( "name" to "{binding.name.text.toString()}", "email" to "{binding.email.text.toString()}")

  2. Better assign val name = binding.name.text.toString() and then hashMapOf( "name" to name)

  3. Even better to create a custom object and set the values.

    data class UserInfo (val name: String? = "",val email: String? = "") { constructor(): this("","")}