您可以使用不可見的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(?) –