Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOnLongClickListener in android with kotlin

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?

like image 744
Nour Medhat Avatar asked Aug 22 '17 09:08

Nour Medhat


People also ask

How do I get button ID in Kotlin?

1 Answer. Show activity on this post. You call findViewWithTag() instead of findViewById() in your code. Also you are not doing string interpolation correctly by var idString = "Button%i" .

How do I use onClick Kotlin?

Inside the onClick function, we use the Kotlin when statement, which is equivalent to switch in other languages. For the onClick function to be triggered, you must register the setOnClickListener over the button with the interface using the context( this ).


1 Answers

Try this in your activity class

lv.setOnItemClickListener { parent, view, position, id ->
    Toast.makeText(this, "Position Clicked:"+" "+position,Toast.LENGTH_SHORT).show()
}
like image 52
Sunil P Avatar answered Sep 22 '22 15:09

Sunil P