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

**URL:** <https://discuss.kotlinlang.org/t/making-simple-app-add-names-to-listview-android-kotlin-please-help/17563>\
**Category:** Android\
**Created:** [May 9, 2020, 1:12pm UTC](https://discuss.kotlinlang.org/t/making-simple-app-add-names-to-listview-android-kotlin-please-help/17563 "2020-05-09T13:12:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iMostafa](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@iMostafa](https://discuss.kotlinlang.org/u/iMostafa)\
**Post date:** [May 9, 2020, 1:12pm UTC](https://discuss.kotlinlang.org/t/making-simple-app-add-names-to-listview-android-kotlin-please-help/17563/1 "2020-05-09T13:12:33Z")

</div>

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>

```

---

<div class="post-metadata">

**Author:** ![ebrowne72](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@ebrowne72](https://discuss.kotlinlang.org/u/ebrowne72)\
**Post date:** [May 13, 2020, 6:48pm UTC](https://discuss.kotlinlang.org/t/making-simple-app-add-names-to-listview-android-kotlin-please-help/17563/2 "2020-05-13T18:48:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ptdede](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ptdede/32/7530_2.png) [@ptdede](https://discuss.kotlinlang.org/u/ptdede)\
**Post date:** [May 14, 2020, 3:35am UTC](https://discuss.kotlinlang.org/t/making-simple-app-add-names-to-listview-android-kotlin-please-help/17563/3 "2020-05-14T03:35:41Z")

</div>

Use viewholder pattern.

in your adapter

```auto
  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

```auto
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.
