从嵌套 recyclerview 的所有输入框中获取所有编辑文本是获取最新的 recyclerview 项目而不是所有数据

Fetching all edit text from all input box of nested recylerview is fetching latest recylerview item instead all data

我在此处添加代码,我试图在 textview click 上获取所有输入字段值(所有嵌套的 recyler 视图项)。因此,如果我有 2 个父 recylerview 和第一个父 3 个子回收器项目以及第二个父 recylerview 4 个子项目。那么我如何在 textview 单击父项或从 mainactivity 中获取所有 7 个项目详细信息。我只得到最后添加的 4 个项目,而没有获取第一个父项目。我缺少什么?提前致谢。

//Main class
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    createMockData()

}

@SuppressLint("WrongConstant")
private fun bindDataWithUi(itemData: ArrayList<HashMap<String, String>>) {
    // Create vertical Layout Manager
    val locationDatesList = findViewById<RecyclerView>(R.id.locationDatesList)
    val tv = findViewById<TextView>(R.id.tv)

    locationDatesList.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)

    // Access RecyclerView Adapter and load the data
    val adapter = MainAdapter(itemData)
    locationDatesList.adapter = adapter
    tv.setOnClickListener(View.OnClickListener {
        try {
            var data = adapter.getAllItemDetails()
            for(result in data){
                System.out.println(result)
            }
        }catch (e:Exception){
            e.printStackTrace()
        }

    })
}

private fun createMockData() {
    // Initialize test locations
    val items: ArrayList<HashMap<String, String>> = ArrayList()
    val time = System.currentTimeMillis()
    // Load items into ArrayList
    items.add(hashMapOf("A" to "B", "C" to "D", "time" to time.toString()))

    val tomorrowTime = time + (1000 * 60 * 60 * 24 * 3)
    items.add(hashMapOf("E" to "F", "G" to "H", "time" to tomorrowTime.toString()))
     // Bind items to RecyclerView
    bindDataWithUi(items)
}


}

//父适配器

class MainAdapter(var locationList: ArrayList<HashMap<String, String>>): 
RecyclerView.Adapter<MainAdapter.ViewHolder>() {

val byDates = locationList.groupBy { it["time"] }
lateinit var childAdapter : PropertyAdapter
lateinit var parentItemHolder: MainAdapter.ViewHolder
@SuppressLint("WrongConstant")
override fun onBindViewHolder(holder: MainAdapter.ViewHolder, position: Int) {
    // Update date label
    parentItemHolder = holder
    val sdf = SimpleDateFormat("MM/dd/yyyy")
    val dateList = byDates.values.toMutableList()
    holder.date?.text = sdf.format(dateList[position][0].get("time")?.toLong())
    holder.childRv?.layoutManager = LinearLayoutManager(holder.childRv.context, LinearLayout.VERTICAL, false)
    var properyDataList:ArrayList<propertyDataClass> = ArrayList<propertyDataClass>()

    // Create vertical Layout Manager
    holder.date?.setOnClickListener(View.OnClickListener {
        // Access RecyclerView Adapter and load the data
        properyDataList.add(propertyDataClass())
        childAdapter = PropertyAdapter(properyDataList)
        holder.childRv?.adapter = childAdapter
        childAdapter.updateList(propertyDataClass())
    })
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter.ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_item_dates, parent, false)
    return ViewHolder(v)
}

override fun getItemCount(): Int {
    return byDates.count()
}

class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
    val date = itemView.findViewById<TextView>(R.id.locationDate)
    val childRv = itemView.findViewById<RecyclerView>(R.id.childList)
}

 interface onClickItemAction{
    public fun productItemList() {

    }
}

fun getAllItemDetails():ArrayList<String>{
    var stringList = ArrayList<String>()
    try {
        for (i in 0 until locationList.size) {
            //val view = parentItemHolder.childRv.findViewHolderForLayoutPosition(i)//locationDatesList
              //val view = parentItemHolder.childRv.findViewHolderForAdapterPosition(i);
            val view = parentItemHolder.childRv.findViewHolderForLayoutPosition(i)
                val etLabel: EditText? = view?.itemView?.findViewById(R.id.etLabel)
                val etValue: EditText? = view?.itemView?.findViewById(R.id.etValue)
                //stringList.add(etLabel?.text.toString())
                stringList.add(etValue?.text.toString())

        }
    }catch (e:Exception){
        e.printStackTrace()
    }

    return stringList
}

}
//child adaoter
class PropertyAdapter(
val propertyList: ArrayList<propertyDataClass>,
): RecyclerView.Adapter<PropertyAdapter.ViewHolder>() {
lateinit var vHolder :ViewHolder
override fun onBindViewHolder(holder: PropertyAdapter.ViewHolder, position: Int) {
    vHolder = holder
    val propertyItem = propertyList[position]
    holder.etLabel?.text = propertyItem.inputName
    holder.etValue?.text = propertyItem.inputValue
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PropertyAdapter.ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_item_locations, parent, false)
    return ViewHolder(v)
}

override fun getItemCount(): Int {
    return propertyList.size
}

class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
    val etLabel = itemView.findViewById<TextView>(R.id.etLabel)
    val etValue = itemView.findViewById<TextView>(R.id.etValue)
}

fun updateList(propertyItem: propertyDataClass){
    //propertyList.add(propertyDataClass())
    notifyDataSetChanged()
}

fun getAllItemDetails(pos:Int){
    val propertyListValue = ArrayList<propertyDataClass>()
    for (i in 0 until propertyList.size){
        var  propertyDataClass = propertyDataClass()
    }
}

fun getItemDetailsbyView(rootView: View){
    var stringList = ArrayList<String>()

    for (i in propertyList){
        //val view = vHolder.findViewHolderForLayoutPosition(i)
        //val etLabel: EditText? = view?.itemView?.findViewById(R.id.etLabel)
        //val etValue: EditText? = view?.itemView?.findViewById(R.id.etValue)
        //stringList.add(etLabel?.text.toString())
        //stringList.add(etValue?.text.toString())
    }
}

}

//main.xml

   <?xml version="1.0" encoding="utf-8"?>
  <androidx.constraintlayout.widget.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
<TextView
    android:id="@+id/tv"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:text="@string/app_name"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/locationDatesList"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp"/>
  <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/locationDatesList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp" >

 </androidx.recyclerview.widget.RecyclerView>
 </LinearLayout>

 </androidx.constraintlayout.widget.ConstraintLayout>

//父recylerview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/locationDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:gravity="center_vertical"
            android:text="Date"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:textColor="#009688"
            android:textSize="18sp" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/childList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        </androidx.recyclerview.widget.RecyclerView>

    </LinearLayout>

</androidx.cardview.widget.CardView>

 </LinearLayout>

//子回收视图

  <?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="10dp">

        <TextView
            android:id="@+id/locationBadge"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/ic_launcher_background"
            android:gravity="center_vertical|center_horizontal"
            android:text="✓"
            android:textColor="#fff"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp">

            <EditText
                android:id="@+id/etLabel"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text="Name"
                android:maxLines="1"
                android:editable="false"
                android:focusable="false"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                android:textSize="18sp" />

            <EditText
                android:id="@+id/etValue"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text="Address"
                android:maxLines="1"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                android:textSize="16sp" />

        </LinearLayout>

    </LinearLayout>

    </LinearLayout>

只需要将视图持有者保存在单独的数组列表中,然后按位置查找视图持有者就解决了我的问题。

在主适配器中,我添加并点击后获取。

         viewHolderList.add(holder)


 fun getAllItemDetails():ArrayList<String>{
    var stringList = ArrayList<String>()
    try {
        for (i in 0 until locationList.size) {
            //val view = parentItemHolder.childRv.findViewHolderForLayoutPosition(i)//locationDatesList
              var viewHolder = viewHolderList.get(i)
            for (i in 0 until 3) {
                val view = viewHolder.childRv.findViewHolderForAdapterPosition(i);
                //val view = parentItemHolder.childRv.findViewHolderForLayoutPosition(i)
                val etLabel: EditText? = view?.itemView?.findViewById(R.id.etLabel)
                val etValue: EditText? = view?.itemView?.findViewById(R.id.etValue)
                //stringList.add(etLabel?.text.toString())
                stringList.add(etValue?.text.toString())
            }

        }
    }catch (e:Exception){
        e.printStackTrace()
    }

    return stringList
}