您可以使用不可见的textView来存储id的值。
ArrayList<HashMap<String, String>> hashExample = new ArrayList<>();
Integer id = 1 ;
String value = "value";
HashMap<Integer, String> map = new HashMap<>();
map.put("ID", "" + id);
map.put("VALUE", value);
hashExample.add(map);
String[] from = {"ID", "VALUE"};
int[] to = {R.id.txtID, R.id.txtValue};
adapter = new SimpleAdapter(getActivity(), hashExample,
R.layout.list_item_example, from, to);
ListView list = (ListView) getView().findViewById(R.id.listView);
list.setAdapter(adapter);
检索数据,实现这个在onItemClickListener什么
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Integer id = Integer.valueOf(((Map<String, String>) adapter.getItem(arg2)).get("ID"));
String value= ((Map<String, String>) adapter.getItem(arg2)).get("VALUE");
}
});
希望它能帮助。
你可以尝试使用数据库表ID在每个listView项目上设置标签。然后,你可以调用getTag来确定你想要选择哪一个。 –
按照http://stackoverflow.com/questions/10644142/setting-tags-to-each-item-in-a-listview-in-android(?) –