'Dynamic list of custom items each with edit button. How to button binding?
I tried importing ObjLargeBinding, but idk what to do from there. Do I have to dynamically setOnClickListeners for each objective on screen?
'''
import android.content.ClipData.Item
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.example.android.databoundlist.databinding.ObjLargeBinding
import com.example.android.databoundlist.databinding.QuestEditorBinding
class QuestEditFragment : Fragment() {
    private var _rowbinding:ObjLargeBinding? = null
    private var _binding: QuestEditorBinding? = null
    private var openQuest: Quest? = Quest()
    private var objbindings: List<ObjLargeBinding>? = listOf()
    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!
    private val rowbinding get() = _rowbinding!!
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = QuestEditorBinding.inflate(inflater, container, false)
        for (item in openQuest!!.objectives) {
            val itemBinding: ObjLargeBinding = ObjLargeBinding.inflate(
                layoutInflater,
                container,
                true
            )
            itemBinding.setData(item) 
            itemBinding.buttonEditObj.setOnClickListener {
                findNavController().navigate(R.id.action_QuestEditFragment_to_ObjEditFragment)
        }
        }
        return binding.root
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        /*viewModelRoutesFragment =
            ViewModelProvider(requireActivity()).get(ViewModelRoutesFragment::class.java)*/
    }
    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
'''
Notice: rowbinding, itembinding '''
<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <variable
            name="handler"
            type="com.example.android.databoundlist.ButtonHandler"/>
        <variable
            name="quest"
            type="com.example.android.databoundlist.Quest" />
    </data>
    <LinearLayout
        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"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.android.databoundrecyclerview.MainActivity">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="0px"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="@string/addUser"
                android:onClick="@{() -> handler.addToStart()}"/>
            <Button
                android:layout_width="0px"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="@string/removeUser"
                android:onClick="@{() -> handler.deleteFromStart()}"/>
            <Button
                android:layout_width="0px"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="@string/useLarge"
                android:onClick="@{() -> handler.useLarge()}"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1"
            android:orientation="vertical"
            app:entries="@{quest.objectives}"
            app:layout="@{handler.layout}"/>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="0px"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="@string/addUser"
                android:onClick="@{() -> handler.addToEnd()}"/>
            <Button
                android:layout_width="0px"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="@string/removeUser"
                android:onClick="@{() -> handler.deleteFromEnd()}"/>
            <Button
                android:layout_width="0px"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="@string/useSmall"
                android:onClick="@{() -> handler.useSmall()}"/>
        </LinearLayout>
    </LinearLayout>
</layout>
''' Here is my quest_editor.xml buttonEditObj is not working properly. How to set up onClick correctly? '''
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="questobjective"
            type="com.example.android.databoundlist.Quest.QuestObjective" />
        <variable name="data" type="com.example.android.databoundlist.Quest.QuestObjective"/>
    </data>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="0dp"
        android:paddingBottom="0dp">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:paddingBottom="5dp">
            <TextView
                android:id = "@+id/edit_objective_name"
                android:background="@color/light_background"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:layout_weight = "1"
                android:inputType="textLongMessage|textAutoCorrect"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:text="@={data.obj}"
                android:hint="hyuck"></TextView>
            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <Button
                    android:id="@+id/buttonEditObj"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#673AB7"
                    android:onClick="onClick"
                    android:text="edit" />
            </FrameLayout>
        </LinearLayout>
        <TextView
            android:id = "@+id/edit_objective_description"
            android:background="@color/light_background"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_weight = "1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text = "@={data.desc}">
        </TextView>
         </LinearLayout>
</layout>
''' Finally, the row item with the edit button
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|
