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就能夠了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM