Retrieving Original HashMap from a SimpleAdapter/ListView on Item Press
I have a SimpleAdapter that populates a ListView, but when the user presses an item I'd like to be able to get the original HashMap that was used to populate the row that was clicked. Unfortunately it seems that all I can get back is an Object which only supports toString(). Having the String from the view the user clicked is not useful since it is not a unique identifier.
How can I get my data back for the row the user clicked on?
Arra开发者_StackOverflow中文版yList<HashMap<String, String>> reformedBundle = new ArrayList<HashMap<String, String>>();
// [...]
mResultsAdapter = new SimpleAdapter(this,
reformedBundle, R.layout.resultrow,
new String[] { "ProviderName", "Street", "City" },
new int[] { R.id.tvField1, R.id.tvField2, R.id.tvField3
});
lvResults.setAdapter(mResultsAdapter);
lvResults.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
// Here's where I want to get the original HashMap<String, String> so I can query other keys in the HashMap
}
});
I stored in a member variable.
private ArrayList<HashMap<String, String>> reformedBundle;
...
lvResults.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
HashMap<String, String> map = reformedBundle.get(pos);
}
});
精彩评论