Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't need to get the "row ID of the <code>ListView</code>", you need to get the "row ID of the <em>item</em> that was clicked in the <code>Listview</code>"</p> <p>Luckily, Android gives you the row ID right there as the last parameter of <code>onListItemClick()</code>, where you're currently using the position:</p> <pre><code>public void onListItemClick(ListView parent, View v, int position, long id) { Intent intent = new Intent(this, NewActivity.class); // intent.putExtra("position", position); // not what you want intent.putExtra("id", id); // more likely what you want startActivity(intent); } </code></pre> <p>If you do not have an ID, you can also get the actual item, i.e. a <code>RowData</code> object in your case, from the <code>ListAdapter</code>:</p> <pre><code>public void onListItemClick(ListView parent, View v, int position, long id) { RowData item = (RowData) getListAdapter().getItem(position); Intent intent = new Intent(this, NewActivity.class); intent.putExtra("id", item.mId); startActivity(intent); } </code></pre> <p>In both cases, you have to change your code in <code>NewActity</code> so you extract the ID, not position, from the extra bundle.</p> <p><strong>Another update:</strong></p> <blockquote> <p>Lets say my list have 10 items, item 1, item 2, item 3 and so on. When item 1 is clicked i want that to open a new activity, let's call that NewActivityOne, and NewActivityOne will refer to a layout with some text called layoutone. Item 2 will also open a new activity, NewActivityTwo, which will refer to a new layout called layouttwo. And that's how I want it to be for all my items.</p> </blockquote> <ul> <li><p>If your detail activities differ <em>only in the layout</em> (i.e. not in any behavior/code), just use <code>putExtra()</code> to pass an <code>R.layout.*</code> ID (let's call it "layoutId") to your <code>NewActivity</code> and use that in <code>setContentView(getIntent().getExtras().getInt("layoutId"));</code>.</p></li> <li><p>If your detail activities differ <em>more widely</em>, create a separate <code>Activity</code> class for every list item, and in <code>onListItemClick()</code> create an <code>Intent</code> with the respestive activity class you want to display.</p></li> </ul> <p>In either case, you can store the details to display (<code>int layoutId</code> or <code>Class&lt;Activity&gt;</code>) in your <code>RowData</code> class so you can easily access it in <code>onListItemClick()</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload