ListView onItemClick - I need something more than just the position of the click

by Tobiah » Fri, 14 Jan 2011 05:02:08 GMT


Sponsored Links
 I'm getting a call to my AdapterView.OnItemClickListener() when I
touch an item in my ListView.  The ListView (subclass of) is populated from
a String[] data like this:

this.setAdapter(new ArrayAdapter(this.context, 
android.R.layout.simple_list_item_1, data));

Each row in the data holds an individual's first and last name along
with their database ID#.  Here is what I'm getting back from the call:

parent   The AdapterView where the click happened.
view     The view within the AdapterView that was clicked (this will be a view 
provided by the adapter)
position The position of the view in the adapter.
id       The row id of the item that was clicked.

Now, if I keep a representation of the data set somewhere else in an array, then
I could index that array by the position parameter.  I'd like to get the 
information
from the parameters to the event handler though.  Right now, the 'id' param 
mirrors
the position parameter.  Is that something that I can set somewhere?  Otherwise,
it's ugly, but I could glean the information from the text that is displayed in
each row.  How would I do that given the 'parent' or 'view' parameter.

Thanks,

Tobiah

--



Re: ListView onItemClick - I need something more than just the position of the click

by TreKing » Fri, 14 Jan 2011 05:32:34 GMT


 



 http://developer.android.com/reference/android/widget/ListView.html #getAdapter()


-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 


Sponsored Links


Re: ListView onItemClick - I need something more than just the position of the click

by Kostya Vasilyev » Fri, 14 Jan 2011 05:37:46 GMT


 14.01.2011 0:02, Tobiah :
Now, if I keep a representation of the data set somewhere else in an array, then I could index that array by the position parameter.
The "position" parameter is the position within the adapter, it's the same value passed to getView().
I'd like to get the information from the parameters to the event handler though. Right now, the 'id' param mirrors the position parameter. Is that something that I can set somewhere?
Yes, override YourAdapter.getItemId() and return whatever you need. Can be the "_id" value from the database or anything else. There is another very useful override - YourAdapter.getItem() which is supposed to return the data object for that position. You can get the value outside of your adapter, by calling ListView.getItemAtPosition(). Note that if your data actually comes from a database, you could just use a CursorAdapter to avoid copying data into a memory array.
Otherwise, it's ugly, but I could glean the information from the text that is displayed in each row. How would I do that given the 'parent' or 'view' parameter.
There is also the occasionally useful View.setTag / getTag that lets you associate your own arbitrary data object with any View, and later get the value back somewhere else. You could use setTag() inside your adapter's getView(), then use it somewhere else, as needed. -- Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com --



Re: ListView onItemClick - I need something more than just the position of the click

by Tobiah » Fri, 14 Jan 2011 06:07:53 GMT


 

On Thu, Jan 13, 2011 at 3:02 PM, Tobiah <t...@tobiah.org I'd like to get the information from the parameters to the event handler though. http://developer.android.com/reference/android/widget/ListView.html #getAdapter()
I looked at the ListAdapter docs. It's not clear to me, given the methods available what to do with the Adapter once I have it. At best, I'm assuming that I could somehow pull out the display text given the position of the item? Thanks, Tobiah --



Re: ListView onItemClick - I need something more than just the position of the click

by Kostya Vasilyev » Fri, 14 Jan 2011 06:39:35 GMT


 Yes you can. Call Adapter.getItemAtPosition() with the position value passed
into onClick().

--
Kostya Vasilyev --  http://kmansoft.wordpress.com 
14.01.2011 1:08  "Tobiah" <t...@tobiah.org> :



though.
 http://developer.android.com/reference/android/widget/ListView.html #getAdapter()
available
could somehow pull

-- 



Re: ListView onItemClick - I need something more than just the position of the click

by Tobiah » Fri, 14 Jan 2011 06:45:58 GMT


 

Yes you can. Call Adapter.getItemAtPosition() with the position value passed into onClick().
I'm new to this. I forgot to look at the inherited methods in the docs. What seems most interesting is getItemId(int position). If I could set that Id when creating the Adapter then I'd be all set. Is this the same Id that will be sent to my onItemClick handler? I'm just not sure how to set the Id. Thanks! Tobiah --



Re: ListView onItemClick - I need something more than just the position of the click

by Kostya Vasilyev » Fri, 14 Jan 2011 06:56:40 GMT


 It's the same id as returned by the adapter's getItemId, but I'm not sure
ArrayAdapter returns anything useful there.

I'd actually recommend you implement your own adapter, subclassing from
BaseAdapter.

It's not that difficult, there are plenty of samples and tutorials. And yet
the stuff you can learn this way, getting insight into listview/adapter
interaction, can be quite useful.

--
Kostya Vasilyev --  http://kmansoft.wordpress.com 
14.01.2011 1:46  "Tobiah" <t...@tobiah.org> :


passed into onClick().

-- 



Re: ListView onItemClick - I need something more than just the position of the click

by Tobiah » Fri, 14 Jan 2011 07:16:49 GMT


 

It's the same id as returned by the adapter's getItemId, but I'm not sure ArrayAdapter returns anything useful there. I'd actually recommend you implement your own adapter, subclassing from BaseAdapter. It's not that difficult, there are plenty of samples and tutorials. And yet the stuff you can learn this way, getting insight into listview/adapter interaction, can be quite useful.
I didn't realize that my onItemClick handler would have access to the members in the class in which it was defined. I just stuffed a list of Id values in a String array and index that with the position parameter I'm getting. It's not as pretty as I'd like, and I'm keeping and extra list around that has to be updated when the ListView is updated, but it works. Thanks for the help. Tobiah --



Re: ListView onItemClick - I need something more than just the position of the click

by Kostya Vasilyev » Fri, 14 Jan 2011 07:34:14 GMT


 Yes, that's because it's a non-static inner class, which is a Java feature.
Can be very useful at times :)

--
Kostya Vasilyev --  http://kmansoft.wordpress.com 
14.01.2011 2:17  "Tobiah" <t...@tobiah.org> :


ArrayAdapter returns anything useful there.
BaseAdapter.
yet the stuff you can learn this way, getting insight into
members in the class in
and index that
and I'm keeping
but it works.

-- 



Re: ListView onItemClick - I need something more than just the position of the click

by metal mikey » Fri, 14 Jan 2011 07:54:23 GMT


 


You might find Tags to be handy, you can store meta data for each View
in the List within the Views' tags.

-- 



Other Threads

1. Auto Updating apps in Android 2.2/Froyo & Permission





No. Re-watch the video(s) - they clearly state that auto update only works
if the permissions haven't changed.

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices
http://sites.google.com/site/rezmobileapps/treking

2. Digest for android-security-discuss@googlegroups.com - 2 Messages in 2 Topic

Hi,
I assume you are using 802.1x TLS and you have already imported the
certificate and key in the device using a PFX file.
That PFX contained also the Root CA used to sign the client certificate and
the server certificate you have in your RADIUS.

When setting up the "Enterprise(802.1x) connection you need the following:
EAP: TLS
Phase 2 authenticaiton: None
CA Certificate: Chose the name of the imported PFX file
Client Certificate: Chose the name of the imported PFX file.
Identity: name of the computer as expected by the RADIUS

If your RADIUS is a Microsoft IAS Server you might need to add "host/" in
the Identity and in the CN of the certificate.
Regards
Jofre

On Wed, May 19, 2010 at 8:21 PM, <
android-security-discuss+nore...@googlegroups.com<android-security-discuss%2bnore...@googlegroups.com>



> Group: 

3. Auto Updating apps in Android 2.2/Froyo & Permission

4. Unable to get EAP TLS working on Androi

5. "unable to open database file" after OS upgrade

6. <WTA> Multitouch di Android

7. Closed WTA: email client untuk microsoft exchange