如何在listview(Hashmap)中存储来自OnItemClickListener的值?

时间:2014-03-03 11:56:15

标签: java android hashmap

当我点击listview上的项目时,我希望存储值data1(示例我点击位置0存储a,位置1商店b)我试图了解how do i get the value from the android listview?但我无法存储值从此。

final ArrayList<HashMap<String, String>> ArrList = new ArrayList<HashMap<String, String>>();
ListView list1 = (ListView) findViewById(R.id.listView1);

String[] data1 = { "a", "b" };
String[] data2 = { "1", "2" };

for (int i = 0; i < data1.length; i++) {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("Name", data1[i]);
    map.put("Datas", data2[i]);
    ArrList.add(map);
}

ListAdapter adapters = new SimpleAdapter(MainActivity.this, ArrList, R.layout.activity_column, new String[] { "Name", "Datas" }, new int[] {
            R.id.textView1, R.id.textView2 });
list1.setAdapter(adapters);

list1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String, String> map = ArrList.get(position);
        String result = map.toString();
    }
});

1 个答案:

答案 0 :(得分:0)

使用keySet()获取map

中的所有值
for (String key: map.keySet()) {

System.out.println("key : " + key)
System.out.println("value : " + map.get(key));
}

并获得特定值。

 map.get(key);

但我们无法使用positon获取hapmap