0

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:

  1. What do I need the parent, view and id for?

  2. If I click the 3rd element, the position (int) sent I expect to be 2 if the list is 0-index based, right?

  3. 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.

  1. 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...

Sami
  • 393
  • 8
  • 22

1 Answers1

0

Say that your ListView contains the headers of some news articles.
By clicking on a list item you want to be redirected to a details activity where you can read the full text of the article.
All headers and articles are stored in a List of Article objects, each containing the header and the text of the article.
How will you identify which article's header was clicked?
This is the purpose of the arguments position and id.
Depending on your needs you will choose one or the other, so you will fetch from the List the article's text and pass it as an extra to the Intent that opens the details activity.

You know that you can create very complex ListViews where each item is a container of multiple views or a ListView itself.
This is when you will need the parent and view arguments.

All this is application specific.

If you don't need it fine, but the arguments are there for you to use if and when you will need them.

Now for:

If I wanted to send a Bundle with more complex information

and

Still this doesn't clarify how to send additional complex info to the DetailedActivity

This is your responsibility to construct a Serializable or Parcelable Bundle and put it as an extra to the Intent and has nothing to do with the available arguments of setOnItemClickListener.
There are a lot of examples here in SO and other sites for serializing objects to put as extras, like
Cannot pass custom Object in an Intent: The Method Put Extra is Ambiguous for the type Intent
and
How to send an object from one Android Activity to another using Intents?
Follow the standards and you will be able to send this complex info via intent.

forpas
  • 160,666
  • 10
  • 38
  • 76