Making simple app add names to listView(Android+Kotlin)please help

I’m trying to get a listView to have a button that corresponds to each item on the listView. For example, if I have a product in the list, i want to click the button and display the information for that specific product when i click the button. How can i add an on click listener in the adapter for my button so that it works according to each item in the listview?

This is my custom array adapter.

MainAc

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)




            val listView = findViewById<ListView>(R.id.listView)
            val editText = findViewById<EditText>(R.id.nameText)
            val button = findViewById<Button>(R.id.button)

            listView.adapter = MyCustomAdapter(this, populateArrayList(),editText,button)



    }



    private fun populateArrayList() : ArrayList<String>{
        val myArrayList = ArrayList<String>()
        myArrayList.add("S")
        myArrayList.add("Anakin")
        myArrayList.add("Obi-Wan")
        return myArrayList
    }

    private class MyCustomAdapter(private val context: Context, private val myList: ArrayList<String>,val editText: EditText,val button: Button):BaseAdapter() {

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
            val rowLayout = LayoutInflater.from(context).inflate(R.layout.rows,parent,false)
            val countRow = rowLayout.findViewById<TextView>(R.id.rowNumber)
            val nameText = rowLayout.findViewById<TextView>(R.id.name_textView)

            button.setOnClickListener {
                nameText.text = editText.toString()
                countRow.text = "Row :$position"

            }

            return rowLayout
        }

        override fun getItem(position: Int): Any {
            return position
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getCount(): Int {
            return myList.size
        }
    }

}

MainActivity

> <?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">
> 
> 
>     <ListView
> 
>         android:id="@+id/listView"
>         android:layout_width="394dp"
>         android:layout_height="545dp"
>         android:layout_marginStart="8dp"
>         android:layout_marginEnd="8dp"
>         android:layout_marginBottom="16dp"
>         app:layout_constraintBottom_toBottomOf="parent"
>         app:layout_constraintEnd_toEndOf="parent"
>         app:layout_constraintHorizontal_bias="0.0"
>         app:layout_constraintStart_toStartOf="parent" />
> 
>     <EditText
>         android:id="@+id/nameText"
>         android:layout_width="wrap_content"
>         android:layout_height="wrap_content"
>         android:layout_marginStart="105dp"
>         android:layout_marginTop="49dp"
>         android:layout_marginEnd="93dp"
>         android:ems="10"
>         android:hint="  Add Your Name"
>         android:inputType="textPersonName"
>         android:textColorHint="#E31313"
>         app:layout_constraintEnd_toEndOf="parent"
>         app:layout_constraintStart_toStartOf="parent"
>         app:layout_constraintTop_toTopOf="parent" />
> 
>     <Button
>         android:id="@+id/button"
>         android:layout_width="wrap_content"
>         android:layout_height="wrap_content"
>         android:layout_marginTop="12dp"
>         android:text="Button"
>         app:layout_constraintEnd_toEndOf="parent"
>         app:layout_constraintHorizontal_bias="0.498"
>         app:layout_constraintStart_toStartOf="parent"
>         app:layout_constraintTop_toBottomOf="@+id/nameText" />
> </androidx.constraintlayout.widget.ConstraintLayout>

rows.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">


    <TextView
        android:id="@+id/name_textView"
        android:layout_width="239dp"
        android:layout_height="94dp"
        android:fontFamily="sans-serif"
        android:includeFontPadding="true"
        android:text="Name"
        android:textColor="#0B0B0B"
        android:textColorHint="#000000"
        android:textSize="16sp"
        android:textStyle="bold|italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.159"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteY="60dp" />

    <TextView
        android:id="@+id/rowNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"

        android:text="TextView"
        app:layout_constraintTop_toBottomOf="@+id/name_textView"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="36dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
1 Like

This forum is for Kotlin questions, not general Android development questions. You would have more luck asking your questions somewhere else.

Use viewholder pattern.

in your adapter

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SomeViewHolder {
    val layout = LayoutInflater.from(parent.context)
      .inflate(R.layout.rows, parent, false)

    return SomeViewHolder(layout, parent.context)
  }

  override fun onBindViewHolder(holder: SomeViewHolder, position: Int) {
    holder.bind(myList[position])
  }

in SomeViewHolder.kt

class SomeViewHolder (
  private val view: View,
  private val context: Context
) : RecyclerView.ViewHolder(view) {

   fun bind(data: String) {
     itemView.yourButtonId.setOnClickListener {
         itemView.nameTextId.text = itemView.editTextId.toString()
         itemView.countRowId.text = "Row :$position"
     }
   }
}

Hope this helps.