Get the text of a dynamically created textfield in android studio (Kotlin)

时间:2018-03-25 19:36:57

标签: android firebase kotlin

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)

1 个答案:

答案 0 :(得分:1)

有两种方法可以解决这个问题:

您正在将EditText添加到LinearLayout,因此您可以对其子项进行迭代 - 如here所述,从而替换while循环。

第二种方法是将EditText添加到List<EditText>并迭代它,替换while循环。

第一种解决方案可能更清洁,但它们都以非常相似的方式工作。

希望这能帮到你!