Home > database >  Checking for null in edit text with for loop
Checking for null in edit text with for loop

Time:03-22

I want to check for empty values ​​in edit text using for loop, I don't want to repeat code while doing this. I tried this but got an error. Can you help me

binding.apply {

  val editTextList: ArrayList<EditText> = ArrayList()
  editTextList.add(binding.etRegistedFullname)
  editTextList.add(binding.etRegistedEmail)
  editTextList.add(binding.etRegistedAge)
  editTextList.add(binding.etRegistedPassword)
  editTextList.add(binding.etRegistedConfirmpassword)
  editTextList.add(binding.etRegistedPhone)

  //Bu for dögüsü sayaseinde 15 satır koddan kurtuldum
  for (item in editTextList) {
    // Toast.makeText(this@RegisterActivity,item.id.toString(),Toast.LENGTH_SHORT).show()
    when {
      TextUtils.isEmpty(editTextList.get(item.id).text.toString().trim { it <= ' ' }) -> {
        editTextList.get(item.id).error = "Cannot be blank"
        editTextList.get(item.id).requestFocus()
      }
      else -> {
        if (isValidPassword(etRegistedPassword.text.toString().trim())) {
          if (etRegistedPassword.text.toString().equals(etRegistedConfirmpassword.text.toString())) {
            val user = getPerson()
            authcreateUser(user)
          } else {
            Toast.makeText(
              this@RegisterActivity, 
              "password and confirm password must be the same",
              Toast.LENGTH_SHORT
            ).show()
          }
        } else {
          etRegistedPassword.setError("At least one number, one lowercase letter, at least one uppercase letter, no spaces and a minimum of 8 characters")
          etRegistedPassword.requestFocus()
        }
      }
    }
  }

}

CodePudding user response:

Since you're code is already wrapped in binding.apply, you don't have to keep typing binding.. And it's easier to create a list using listOf than what you're doing by creating an empty ArrayList and then filling it one by one.

This code of yours doesn't make sense:

editTextList.get(item.id)

item is already an EditText so you don't need to get it from the list again. List.get is going to look for the item at that index in the array, which has no relation at all to the view's ID.

binding.apply {

  val editTextList = listOf(
      etRegistedFullname,
      etRegistedEmail,
      etRegistedAge,
      etRegistedPassword,
      etRegistedConfirmpassword,
      etRegistedPhone
    )

  //Bu for dögüsü sayaseinde 15 satır koddan kurtuldum
  for (item in editTextList) {
    // Toast.makeText(this@RegisterActivity,item.id.toString(),Toast.LENGTH_SHORT).show()
    when {
      TextUtils.isEmpty(item.text.toString().trim { it <= ' ' }) -> {
        item.error = "Cannot be blank"
        item.requestFocus()
      }
      else -> {
        if (isValidPassword(etRegistedPassword.text.toString().trim())) {
          if (etRegistedPassword.text.toString().equals(etRegistedConfirmpassword.text.toString())) {
            val user = getPerson()
            authcreateUser(user)
          } else {
            Toast.makeText(
              this@RegisterActivity, 
              "password and confirm password must be the same",
              Toast.LENGTH_SHORT
            ).show()
          }
        } else {
          etRegistedPassword.setError("At least one number, one lowercase letter, at least one uppercase letter, no spaces and a minimum of 8 characters")
          etRegistedPassword.requestFocus()
        }
      }
    }
  }

}

I didn't check the logic of your code besides that.

  • Related