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

Posted

技术标签:

【中文标题】从嵌套 recyclerview 的所有输入框中获取所有编辑文本正在获取最新的 recyclerview 项目而不是所有数据【英文标题】:Fetching all edit text from all input box of nested recylerview is fetching latest recylerview item instead all data 【发布时间】:2021-12-24 04:36:52 【问题描述】:

我在这里添加代码,我试图在 textview click 上获取所有输入字段值(所有嵌套的回收器视图项)。因此,如果我有 2 个父 recylerview 和在第一个父 3 个子 recyler 项目内和在第二个父 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_
android:layout_
tools:context=".MainActivity">
 <LinearLayout
 android:layout_
 android:layout_
 android:orientation="vertical">
<TextView
    android:id="@+id/tv"
    android:layout_
    android:layout_
    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_
    android:layout_
    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>

//父recyclerview

<?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_
android:layout_>

<androidx.cardview.widget.CardView
    android:layout_
    android:layout_
    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_
        android:layout_>

        <TextView
            android:id="@+id/locationDate"
            android:layout_
            android:layout_
            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_
            android:layout_
            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>

//子recyclerview

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

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_>

    <LinearLayout
        android:layout_
        android:layout_
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="10dp">

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

        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="horizontal"
            android:padding="10dp">

            <EditText
                android:id="@+id/etLabel"
                android:layout_
                android:layout_weight="1"
                android:layout_
                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_
                android:layout_weight="1"
                android:layout_
                android:gravity="center_vertical"
                android:text="Address"
                android:maxLines="1"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                android:textSize="16sp" />

        </LinearLayout>

    </LinearLayout>

    </LinearLayout>

【问题讨论】:

【参考方案1】:

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

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

         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

【讨论】:

以上是关于从嵌套 recyclerview 的所有输入框中获取所有编辑文本正在获取最新的 recyclerview 项目而不是所有数据的主要内容,如果未能解决你的问题,请参考以下文章

如何将所选项目从 recyclerview 显示到文本输入

从 Firestore 中检索嵌套对象并放入 Android 中的 RecyclerView?

ScrollView嵌套Recyclerview显示不全问题

从熊猫数据框中提取嵌套字典

计算数据框中特定列(SUM、AVG、STDEV)的所有嵌套级别聚合

在recyclerview的嵌套recyclerview中的嵌套列表中添加项目