Android 取得 ListView中每个Item项目的值

首先我们须要创建 ListView 。这里假定我们已经创建好了而且使用SimpleAdapter设置好了adapter数据,看一下我们的adapter
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

for (int i = 0; i < 10; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("shopName", "毛家饭店");
map.put("shopAddr", "第" + i + "行内容");
list.add(map);
}

adapter = new SimpleAdapter(this, list, R.layout.list_item_test,
new String[] { "shopName", "shopAddr" }, new int[] {
R.id.shopName, R.id.shopAddr });
接下来我们操作 listview的单击事件
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
HashMap<String, String> map = (HashMap<String, String>) parent
.getItemAtPosition(position);
Toast.makeText(view.getContext(), map.get("shopName"),
Toast.LENGTH_SHORT).show();
}
});
这样我们就能得到商家的名称了,同一时候假设须要获取其他字段内容,仅仅要更改 map 的Key就能够了。
原文地址:https://www.cnblogs.com/yfceshi/p/7052433.html