2

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>
hing ing
  • 23
  • 2

1 Answers1

0

There will be many number_edit_text as you click button. So you should call setText on newly added number_edit_text. And you need to call with UI Thread.

So you must call like below

rowView.findViewById(R.id.number_edit_text).run{
    post{ setText("asdf")}
}

  • this onAddField is called when button is clicked -- so it is in the UI thread, no post is needed, just rowView.findViewById(R.id.number_edit_text).setText("asdf") – AIMIN PAN Aug 03 '21 at 06:24
  • I'd like to say guarante using UI Thread in onAddField() is important rather then call onAddField() in UI Thread. Because onAddField have responsibility to add view and update text by itself. onAddField should be able invoked everywherer. If this is real my work as code reviewer, I will comment like this. :) – user3811067 Aug 03 '21 at 11:47