我有一个填充ListView的SimpleAdapter,但是当用户按下一个项目时,我希望能够获得用于填充所单击行的原始HashMap。不幸的是,我所能得到的只是一个只支持toString()的Object。从用户单击的视图中获取字符串是没有用的,因为它不是唯一标识符。
如何为用户点击的行恢复数据?
ArrayList<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
}
});
答案 0 :(得分:2)
我存储在一个成员变量中。
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);
}
});