0

So I'm trying to figure out how I would get the text from a dynamically created EditText field. This is the code for the dynamic Text fields

private fun AddToDoItem() {
  val EditText = EditText(this)
  EditText.gravity = Gravity.TOP
  EditText.gravity = Gravity.CENTER
  EditText.tag = "ExtraField" + i
  LinearLayout?.addView(EditText)
  i++
}

And this is the code where I want to get the Textfields text

    Finish.setOnClickListener {
        var x = 0
        val userId = mAuth.currentUser!!.uid
        val mcDatabase = FirebaseDatabase.getInstance().getReference()
        mcDatabase.child("Users").child(userId).child(ToDoName.text.toString()).push()

        while (x < i) {
            val currentUserDb = mDatabaseReference!!.child(userId).child(ToDoName.text.toString())
            currentUserDb.child(i.toString()).setValue("ExtraField" + x.text) //HERE IS WHERE I WANT TO SET THE TEXT
            x++
            Toast.makeText(this, "Finished.", Toast.LENGTH_LONG).show()
        }
    }

Where its commented like "//HERE IS WHERE I WANT TO SET THE TEXT" is where I want the .text string.

(It's in the while loop)

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
StarToLeft
  • 35
  • 1
  • 11

1 Answers1

2

There are two ways to handle this:

You are adding the EditText's to a LinearLayout therefore you can iterate over its children - as described here and therefore replacing the while loop.

The second way would be to add the EditText's to a List<EditText> and iterate over it, replacing the while loop.

The first solution would probably be cleaner, but both of them work in a very similar fashion.

Hope this helps you!

Rene Ferrari
  • 4,096
  • 3
  • 22
  • 28