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. Problem in connecting to Bluetooth devices

Hi all,

I am using the latest cupcake build. When I start Bluetooth (BT) UI
and inquire for devices, a few strange things are happening:

1. Sometimes Inquiry does not show any devices on the UI. Upon
debgging with help from hcidump, I see that a inquiry command is sent
and some devices are found. After this, inquiry complete is received
on the host side. But, no remote name request goes out from the UI.
This is strange. After I reboot, everything works fine for sometime.

2. If I try to connect (phone/media connection) to any headset, I am
able to pair successfully with the device. But, the service level
connection always fails (Error message: Unable to connect to
<DEVICENAME>). Do I need to set any device info / pairing information
in any specific location? Bluez usually had a pincodes file, but I do
not see any such thing for Android.

Maybe I am missing certain .conf file setting or some other setting.
Can someone help? The second issue is obviously troubling me more.

Thanks,
Sidharth
--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

2. Calling webview.clearCache() will clear all caches on device?

Hi,
I have a question when calling webview.clearCache() method. In the SDK
guide, it said it clears cache for all WebView. The default web
browser on Android also use webview, does that mean once I call
clearCache() in my app, even the cache for default sysapp web browser
will also be wiped out? Or this clear cache only applies to all
webviews (if there is more than one) belonged to the app calling it?
Thanks,
Mimu
--~--~---------~--~----~------------~-------~--~----~

3. Can I catch the air interface signalling in Android's platform

4. Update on Android Developer Challenge 2

5. Defaulting to numeric keyboard on EditText without forcing numeric input.

6. display an SVG file

7. How can i install at Fedora Eclipse the Android Plugin ADT