Java Android listview适配器获取项目id而不进行设置
我是Android新手,我有Adapter-Java Android listview适配器获取项目id而不进行设置,java,android,Java,Android,我是Android新手,我有Adapter-ListView模型来显示数组 我将其值设置为: ListAdapter adapter = new SimpleAdapter(this, contactList, R.layout.bank_list, new String[] { TAG_NAME, TAG_central_office_address }, new int[] { R
ListView
模型来显示数组
我将其值设置为:
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);
}
});
ListAdapter=new simpledapter(此,联系人列表,
R.layout.bank_列表,
新字符串[]{TAG_NAME,TAG_central_office_address},新int[]{
R.id.银行名称,R.id.中央办公室地址});
setListAdapter(适配器);
//选择单个ListView项
ListView lv=getListView();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、android.view.view、,
内部位置,长id){
Intent in=新Intent(getApplicationContext(),BankExchangerListActivity.class);
in.putExtra(“银行id”,标签id);
星触觉(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();
}
url=”http://192.168.1.4:3000/banks.json";
//ListView的Hashmap
ArrayList contactList=新建ArrayList();
//创建JSON解析器实例
JSONParser jParser=新的JSONParser();
//从URL获取JSON字符串
JSONObject json=jParser.getJSONFromUrl(url);
试一试{
//获取联系人数组
banks=json.getJSONArray(TAG_banks);
//通过所有触点循环
对于(int i=0;ivalue
地图放置(标签标识,标识);
地图放置(标签名称、名称);
地图放置(标签、中央办公室、地址、名称);
//将哈希列表添加到ArrayList
联系人列表。添加(地图);
}
}捕获(JSONException e){
e、 printStackTrace();
}
正如您在JSON中看到的,我有ID字段,但我没有在列表视图中设置它。对于下一个查询,我单击ListView
元素并转到其他活动,但我需要获取该元素的TAG\u ID
。也许我做错了什么?如何获取我单击的元素的标记ID
,而不在列表视图中显示它
只需发送已单击项目的标记\u id onItemClick
方法的position
参数给出了自适应列表中所选项目的索引。因此,您可以在以下位置找到原始项目的标签ID:
((Map<String, String>) adapter.getItem(position)).get(TAG_ID)
希望这对你有帮助。
您可以获取列表selecteditemid的位置
,并通过意图
传递它,然后在第二个活动
中获取它,现在在这里获取第一个活动
HashMap
并定义它静态
现在使用foreachloop从HashMap
like获取所有值
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
}
现在,使用ListSelectedItemId
的特定位置,您可以从currentList中获取值
或者
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
for(Map.Entry:Map.entrySet()){
String key=entry.getKey();
对象值=entry.getValue();
// ...
}
你想写在哪里/becouse now helper让我在SimpleAdapter中写getItem方法:嗯。。。Intent in=新Intent(getApplicationContext(),BankExchangerListActivity.class);字符串a=((Map)getItem(position)).get(TAG_ID);in.putExtra(“银行id”,标签id);星触觉(in);现在无法处理ListSelectedItemId的特定位置您可以从currentList获取值如何获取?如果您需要相同的活动,可以使用上述代码在Listview项选择方法中获取,否则需要在SecondActivity中传递所有Hasmap对不起,我的“nubbism”,我需要在第二个活动中传递它,我从第一个开始传入。putExtra(“Bank\u id”,TAG_ID);,但我不明白你在我上面的代码中的什么地方粘贴它?)把它公之于众静态HasMap
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// ...
}