7

How can I use setOnItemClickListner in each item in my ListView?

my xml :

<ListView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</ListView>

this my adapter class

inner class mo3d1Adapter : BaseAdapter {
    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

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

    var listOfMkabala = ArrayList<MeetingDetails>()
    var context: Context? = null

    constructor(context: Context, listOfMkabaln: ArrayList<MeetingDetails>) : super() {
        this.listOfMkabala = listOfMkabaln
        this.context = context
    }

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        val mo3d = listOfmo3d[p0]

        var inflatormo3d = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var myViewmo3d = inflatormo3d.inflate(R.layout.fragment_item, null)

        lvMo3d.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, i, l ->
            Toast.makeText(context, "   TEST STACK   ", Toast.LENGTH_LONG).show()

        }


        myViewmo3d.meeting_name.text = mo3d.name1!!
        myViewmo3d.meeting_date.text = mo3d.date.toString()!!
        myViewmo3d.attendance_number.text = mo3d.n2.toString()!!



        return myViewmo3d


    }

    override fun getItem(p0: Int): Any {
        return listOfmo3d[p0]

    }


}
  • I want listener for each item in my ListView

And when I used this method setOnClickListener in adapter it's not working, where can I use?

Nour Medhat
  • 567
  • 4
  • 9
  • 15

3 Answers3

9

Try this in your activity class

lv.setOnItemClickListener { parent, view, position, id ->
    Toast.makeText(this, "Position Clicked:"+" "+position,Toast.LENGTH_SHORT).show()
}
Maveňツ
  • 1
  • 12
  • 50
  • 89
Sunil P
  • 3,698
  • 3
  • 13
  • 20
1

Although a little quirky this works fine for me.

    latestMessagesAdapter.setOnItemLongClickListener { item, view ->
        val row = item as LatestMessageRow
        return@setOnItemLongClickListener(true)
    }
dave o grady
  • 788
  • 8
  • 12
0

First of all I would like to tell that it is RecyclerView rather than ListView. You can find plenty information why to do in such. For example you can read it hear :

RecyclerView vs. ListView

Regarding your question how to do it in correct way with RecyclerView.

Insert dependencies with RecyclerView, they are now in support library in Kotlin.

implementation "com.android.support:appcompat-v7:25.4.0"

First change your ListView with RecyclerView in xml layout like this:

<android.support.v7.widget.RecyclerView
    android:id="@+id/accountList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

Create Adapter for RecyclerView:

class AccountListAdapter(val accountList: AccountList, val itemListener: (Account) -> Unit) :
    RecyclerView.Adapter<AccountListAdapter.ViewHolder>(){

    override fun getItemCount(): Int = accountList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) =
        holder.bind(accountList[position])


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

    class ViewHolder(itemView: View, val itemClick: (Account) -> Unit): RecyclerView.ViewHolder(itemView){
        fun bind(account : Account){
            with(account){
                itemView.accountName.text = title
                itemView.setOnClickListener{ itemClick(this)}
            }
        }
    }
}

item_account.xml

<?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">

    <TextView
        android:id="@+id/accountName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Models (in Kotlin you can put them in one file and name for example AccountModels.kt) :

data class AccountList(val accounts: List<Account>){

    val size : Int
        get() = accounts.size

    operator fun get(position: Int) = accounts[position]
}

data class Account(val id : Long, val title : String, val balance : Int, val defCurrency: Int)

In Fragment/Activity connect your Adapter to RecyclerView:

override fun onStart() {
    super.onStart()
    setupAdapter()
}

fun setupAdapter(){

    Log.d(TAG, "updating ui..")
    val account1 = Account(1,"Credit", 1000, 2)
    val account2 = Account(2, "Debit", 500, 2)
    val account3 = Account(3, "Cash", 7000, 2)

    val accounts : List<Account> = listOf(account1, account2, account3)

    val adapter = AccountListAdapter(AccountList(accounts)){
        val title = it.title
        Log.d(TAG, "$title clicked")
    }
    accountList.layoutManager = LinearLayoutManager(activity)
    accountList.adapter = adapter
}

That is all. Everything should work now. Hope it helps.

aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
  • This answer does not directly answers the question, please modify as per in the question description. Thanks – blueware Jan 13 '19 at 14:18