views:

66

answers:

4

Hello.

I'm developing an Android application.

I have several objects loaded on a ListActivity. I want to know the item clicked on event onListItemClick.

I see method onListItemClick has a parameter called id. How can I setup this id?

My objects are identified by an ID, how can I add this ID to listItem?

Thanks.

A: 

Use the following;

listView.getItemAtPosition(position);

Where listView is the name of your list view.

Mudassir
No, it doesn't work for me. For example, I put object with id = 1 on position 12. I don't want to know the position, I want to know the id from the object on an specific position.
VansFannel
A: 

What is the source of your list data? If you are using a cursor - then the id passed in 'onListItemClick(ListView l, View v, int position, long id)' will automatically be the id of the cursor row.

Dave
I'm not using a cursor. I'm using a String array.
VansFannel
Then assuming your array contains the ID in one of its indexis, access the array using the position variable. i.e. myArray[position][0] (if your ID is @ index 0 of the array)
Dave
Do you see what I mean? The source data must have your objects ID present to begin with, whether it be a String[] or ArrayList, or cursor or any other source.
Dave
It can be a solution. Thanks.
VansFannel
Hi Vans - please mark my answer as accepted if it helped you solve your problem. Thanks.
Dave
A: 

In this case, just access the id. The id you will get, will the id of selected item. (Remember you are using the onItemClickListener, which will be fired only when user clicks on the list item and returns the clicked item.)

Mudassir
Yes, but I think I have to setup the id for each row, isn't it?
VansFannel
Yeah, you have to set it up.
Mudassir
And, how can I set it up?
VansFannel
A: 

if SectionObj is your object that you want to access later, set that in the adapter when you set the source.

ArrayList<SectionObj> li

ArrayAdapter<SectionObj> adapter=new ArrayAdapter<SectionObj>(getApplicationContext(),android.R.layout.simple_list_item_1, li);
    setListAdapter(adapter);

then in ur listener method..

protected void onListItemClick(ListView l, View v, int position, long id) {

SectionObj o=(SectionObj)getListView().getItemAtPosition(position);

}

franklins