Kotlin Get the value of an EditText

903 Views Asked by At

I am working with Kotlin ANKO program a list with textview and EditText whose identification name I put in the TAG since I do not have the name of the fields. It's all dynamic, at that moment I don't know what each EditText is called, I have the data in a variable.

My problem is trying to get the value of that EditText to save it.

The code that generates the element is like this

editText {
      hint = value --> ES EL VALOR ACTUAL EN LA DB 
      tag = col --> ESTA ES LA VARIABLE QUE TIENE EL NOMBRE POR EJEMPLO APELLIDO
      gravity = Gravity.END
      textSize = 18f
      isEnabled = estate
      textAlignment = View.TEXT_ALIGNMENT_TEXT_START
}.lparams(width = 0, height = wrapContent)
{
   weight = 0.7f
}

So when pressing the save button I need to retrieve the value of each EditText of the view

As the name of the fields I have saved what I do is a FOR of them and try to bring me the value of the corresponding EDITTEXT but it gives me an error, it doesn't really tell me anything but it takes me out of the APP

for (col in columnNames){
  if (col != "ID") {
        var inputTestText = findViewWithTag<EditText>(col).text
  }
}

col => tiene el valor correspondiente, por ejemplo "apellido" pero cuando paso por esa línea directamente se va de la APP sin largar ningún error

1

There are 1 best solutions below

0
Md. Asaduzzaman On

Use RootView where you have added all those EditText to get the specified view with findViewWithTag. It looks like:

var inputTestText = rootView.findViewWithTag<EditText>(col).text