Save data to firestore in android studio using kotlin - Joklinz-Tech

Latest

Home Of Tech news,jobs and android tutorials

Thursday, 8 February 2018

Save data to firestore in android studio using kotlin





referencing the buttons and edittext

val username = findViewById<View>(R.id.editT1) as EditText
val phone = findViewById<View>(R.id.editText6) as EditText
val track = findViewById<View>(R.id.editText2) as EditText
val btn = findViewById<View>(R.id.btn) as Button

Validate the edittext to check for android and web,if false button will be disabled

track.setOnFocusChangeListener { view, b ->    if (track.text.toString().trim().contains("android") || track.text.toString()
    .trim().contains("web")){

        btn.isEnabled=true    }
    else {
        toast("You must type android or web in lowercase")
        btn.isEnabled=false    }
}

onclick on the button will send all the data's in ma fields to firestore database

btn.setOnClickListener {    if(username.text.toString().isBlank() && phone.text.toString().isBlank() 
    && track.text.toString().isBlank()){
        toast("Dont leave any field empty")
        btn.isEnabled=false    }
    else    {
        btn.isEnabled=true    }
    val users = HashMap<String, Any>()
    users.put("username", username.text.toString().trim())
    users.put("email", intentemail)
    users.put("phone", phone.text.toString().trim())
    users.put("time",FieldValue.serverTimestamp())
    users.put("track",track.text.toString().trim())
    users.put("id", intentid)
    toast("Processing your form")
    db.collection("andusers").document("users")
            .set(users)
            .addOnSuccessListener {                toast("Profile Updated Successfully")

            }            .addOnFailureListener { toast("Updating Profile Failed") }
}




No comments:

Post a Comment