I’m building a listview (with 2 lines for each item) from a database query. I’m attaching some data to each item, and I’m having trouble retrieving it again later in their onClick listeners.
data is a list of key/value pairs, let’s say Title, Date, and ID
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
my adapter to create the listview that looks like this:
SimpleAdapter adapter = new SimpleAdapter(this, data,
android.R.layout.simple_list_item_2,
new String[] {"title", "date"},
new int[] {android.R.id.text1, android.R.id.text2});
itemlist.setAdapter(adapter);
setListAdapter(adapter);
I am attaching onListItemClick listeners like so:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(this, CtestActivity.class);
Object obj = this.getListAdapter().getItem(position);
// here I want to extract the id from obj
// obj.toString() is {id:7, title: sometitle, date: yesterday}
}
This may simple be a lack of java knowledge. Either way, I’d appreciate some info on how to access specific values from this ‘map-like’ object obj.
In your
onListItemClick()listener, simply convert the generic Object into your Map: