1

我是 Android 新手,我有适配器 -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);
            }

        });

我从 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!

4

2 回答 2

1

该方法的position参数onItemClick给出了选定项目在适配列表中的索引。因此,您可以在以下位置找到原始商品的 TAG_ID

((Map<String, String>) adapter.getItem(position)).get(TAG_ID)

使适配器最终,以便您可以在侦听器中引用它:

final ListAdapter adapter = ...
于 2013-04-10T10:12:57.670 回答
1

希望这对您有所帮助。你可以得到PostionListSelectedItemId 并通过它现在Intent得到它在SecondActivity这里得到FirstActivity HashMap它定义它static

HashMap 现在使用 foreachloop 从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();
    // ...
}
于 2013-04-10T10:18:12.797 回答