Harshad Raut
Harshad Raut

Reputation: 21

Data is corrupted while storing it into database

As creating a form that stored the candidate's basic info along with a pic, as I click on upload btn data which is entered in edit text does not match data stored in firebase.

upload activity

 binding.btnUpload.setOnClickListener {
    
        showProgressBar()

        val name= binding.etName.toString()
        val fathers_name =binding.etFatherName.toString()
        val gender=binding.etGender.toString()
        val dob=binding.etDob.toString()
        val time_place = binding.etTimePlace.toString()
        val qualification = binding.etQualification.toString()
        val job = binding.etJob.toString()
        val Height = binding.etHeight.toString()
        val fathers_qualification = binding.etFatherQualification.toString()
        val requirement = binding.etRequirement.toString()
        val address = binding.etAddress.toString()
        val contact=binding.etContact.toString()
    //    val imageUrl = imageUri.toString()

        val candidate= Candidates(name,fathers_name,gender,dob,time_place,
            qualification,job,Height,fathers_qualification,requirement,address, contact)

        database.child( System.currentTimeMillis().toString()).setValue(candidate).addOnCompleteListener{
                if (it.isSuccessful){

                    uploadProfilePic()

                }else{

                 hideProgressBar()

                    Toast.makeText(this, "Failed to upload profile", Toast.LENGTH_SHORT).show()
                }




            Toast.makeText(this, "Successfully saved", Toast.LENGTH_SHORT).show()
        }.addOnFailureListener {
            Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show()
        }
    }

}


private fun uploadProfilePic() {
    storageRef= FirebaseStorage.getInstance().getReference("candidates")
    storageRef.putFile(imageUri).addOnSuccessListener {
        Toast.makeText(this, "profile picture uploaded", Toast.LENGTH_SHORT).show()
        hideProgressBar()
    }.addOnFailureListener {

        hideProgressBar()

        Toast.makeText(this, "failed to upload the profile pic", Toast.LENGTH_SHORT).show()
    }

}

Image of corrupted data in firebase

As entered basic details, but it showing some wired data into database.

Upvotes: 1

Views: 67

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598916

When you call this:

val name= binding.etName.toString()

The name variable becomes the string representation of the EditText object itself, not of the text value that the user entered.

To get the actual value, use

val name = binding.etName.getText().toString()

Upvotes: 1

Related Questions