如何获取我点击的列表项数据?

时间:2012-02-11 11:45:51

标签: android list get

我有一些listview和SimpleAdapter

List<HashMap<String, Object>> dataList = new ArrayList<HashMap<String, Object>>();

    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put(ITEMTITLE, getString(R.string.broadcast_street_1_text));
    map.put(ITEMDESCR, getString(R.string.broadcast_request_1_text));
    dataList.add(map);

    map = new HashMap<String, Object>();
    map.put(ITEMTITLE, getString(R.string.broascast_street_2_text)); 
    map.put(ITEMDESCR, getString(R.string.broadcast_request_2_text));
    dataList.add(map);

    SimpleAdapter adapter = new SimpleAdapter(this, 
                                               dataList,
                                               R.layout.broadcast_list_item,
                                               new String[] {ITEMTITLE, ITEMDESCR}, 
                                               new int[] {R.id.broadcast_list_item_title, R.id.broadcast_list_item_descr}); 

    list.setAdapter(adapter);

所以,当我点击某个列表项时,我需要更改活动。

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
            Intent intent = new Intent(getParent(), BroadcastDataActivity.class);
            intent.putExtra("street", arg0.getItemAtPosition(position).toString());
            intent.putExtra("time", arg0.getItemAtPosition(position).toString());
            parentActivity = (WorldTaxiActivityGroup) getParent();
            parentActivity.startChildActivity("BroadcastData", intent);
        }
    });

但是arg0.getItemAtPosition(position)只返回一个数据对象 请帮助我,我怎样才能正确获取数据?

2 个答案:

答案 0 :(得分:8)

使用

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
            Intent intent = new Intent(getParent(), BroadcastDataActivity.class);
            HashMap<String, Object> item = dataList.get(position);
            intent.putExtra("street", item.get(ITEMTITLE).toString();
            intent.putExtra("time", item.get(ITEMDESCR).toString().toString());
            parentActivity = (WorldTaxiActivityGroup) getParent();
            parentActivity.startChildActivity("BroadcastData", intent);
        }
    });

答案 1 :(得分:2)

 public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
            Intent intent = new Intent(getParent(), BroadcastDataActivity.class);
            intent.putExtra("street", dataList.get(position).get("ITEMTITLE"));
            intent.putExtra("time", dataList.get(position).get("ITEMDESCR"));
            parentActivity = (WorldTaxiActivityGroup) getParent();
            parentActivity.startChildActivity("BroadcastData", intent);
        }
    });