4

How to implement an onItemClickListener() on a custom listView in kotlin?_

title_list_view.setOnItemClickListener{ adapterView: AdapterView<*>?, 
    view: View?, position: Int, l: Long ->
}

what to do to start a new activity after clicking an item of a Custom ListView?

Andrei T
  • 2,985
  • 3
  • 21
  • 28
user9483334
  • 237
  • 2
  • 3
  • 7
  • 2
    Possible duplicate of [How to start new activity on button click](https://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click) – user2340612 Mar 17 '18 at 13:03
  • no..it isnt the same thing.... – user9483334 Mar 17 '18 at 18:35
  • Well, to start an `Activity` you should do like that, regardless of the source (a `Button`, a `ListView` or anything else). Then if you get exceptions/other issues it's another story (and you should provide more info on the issue you get) – user2340612 Mar 18 '18 at 12:28

2 Answers2

11

Try this

    title_list_view.setOnItemClickListener { parent, view, position, id ->   

      Toast.makeText(this, "Clicked item :"+" "+position,Toast.LENGTH_SHORT).show()
        Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
        intent.putExtra("position", position);
        this.startActivity(intent);
     }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

You are using kotlin right?

use NextActivity::class.java in Intent

title_list_view.setOnItemClickListener { parent, view, position, id ->   

  Toast.makeText(this, "Clicked item : $position",Toast.LENGTH_SHORT).show()
    Intent intent = new Intent(CurrentActivity.this, NextActivity::class.java)
    intent.putExtra("position", position)
    this.startActivity(intent)
 }
Aniket Bhoite
  • 116
  • 1
  • 6