I want to edit the text of the view that is created every time a button is pressed. However, when running in the app, the text is only modified in the first and last (nth) views, and the second and n-1 views do not change.enter image description here I uploaded a picture showing my current situation.
MainActivity.kt
package com.akj.my_text_rec
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.field.*
class MainActivity : AppCompatActivity() {
private var parentLinearLayout: LinearLayout? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//title = "KotlinApp"
parentLinearLayout = findViewById(R.id.parent_linear_layout)
adkishw.setOnClickListener{
onAddField()
}
}
fun onDelete(view: View) {
parentLinearLayout!!.removeView(view.parent as View)
}
fun onAddField() {
val inflater =
getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val rowView: View = inflater.inflate(R.layout.field, null)
parentLinearLayout!!.addView(rowView, parentLinearLayout!!.childCount - 1)
number_edit_text.setText("asdf")
}
}
field.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/number_edit_text"
android:text="text"
android:textSize="30dp"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="phone" />
<Spinner
android:id="@+id/type_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="right" />
<Button
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:drawable/ic_delete"
android:onClick="onDelete" />
</LinearLayout>