From my LandingClass (which contains a ListView called myList), whenever an item from the ListView is clicked, we want to trigger another activity (DetailedActivity), so the code should look like this:
myList.setOnItemClickListener{ view ->
val intent = Intent(this, DetailedActivity::class.java)
startActivity(intent)
}
Now, if we needed to provide additional (extra) info to the Detailed class, such as the position of the element from the ListView that we just clicked, the code would look like this:
myList.setOnItemClickListener{ parent, view, position, id ->
val intent = Intent(this, DetailedActivity::class.java)
intent.putExtra("thePosition (text)", position)
startActivity(intent)
}
So my question is why in case of sending the extra intent, we need a parent and an id, I don't see them being used anywhere... ?
I don't see some examples in the doc to help me understand. So some questions:
What do I need the parent, view and id for?
If I click the 3rd element, the position (int) sent I expect to be 2 if the list is 0-index based, right?
If I wanted to send a Bundle with more complex information than the position, how would my code (with extra) change? Considering that all params have a defined type, like id is type long. WHat if I wanted to send a paragraph of text instead of the id?
https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener - the official docs showing the method signature
From my research (see above link) I noticed that:
parent AdapterView: The AdapterView where the click happened.
- Why would we need this? Where is the assignment done, or is it implicitly known?
view View: The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position int: The position of the view in the adapter.
id long: The row id of the item that was clicked.
Still this doesn't clarify how to send additional complex info to the DetailedActivity - not a position (int) or an id (long) but maybe an array of complex objects, or a paragraph of text or something else...