我是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