我是Android新手,我有Adapter - ListView
模型來顯示數組。Android listview adapter無需設置它即可獲取物品ID
我設置它的價值是如此:
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.bank_list,
new String[] { TAG_NAME, TAG_central_office_address }, new int[] {
R.id.bank_name, R.id.central_office_address});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, android.view.View view,
int position, long id) {
Intent in = new Intent(getApplicationContext(), BankExchangersListActivity.class);
in.putExtra("Bank_id", TAG_ID);
startActivity(in);
}
});
我得到了我的價值觀從JSON:
url = "http://192.168.1.4:3000/banks.json";
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
banks = json.getJSONArray(TAG_BANKS);
// looping through All Contacts
for(int i = 0; i < banks.length(); i++){
JSONObject c = banks.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String central_office_address = c.getString(TAG_central_office_address);
// Phone number is agin JSON Object
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_central_office_address, name);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
正如你可以在JSON看到我的ID字段,但我並沒有將其設置成ListView
。對於下一個查詢,我點擊ListView
元素並轉到其他活動,但我需要獲得此元素的TAG_ID
。也許我做錯了什麼?如何獲取我單擊的元素的TAG_ID
,而不在ListView
上顯示?
只發送點擊項目的tag_id!
在那裏你的意思是寫/ – brabertaser19 2013-04-10 10:16:11
becouse現在助手說我寫的getItem方法 – brabertaser19 2013-04-10 10:17:13
在SimpleAdapter:http://developer.android.com/reference/android/widget/SimpleAdapter.html#getItem% 28int%29 – flup 2013-04-10 10:18:07