2013-04-10 46 views
1

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

回答

1

onItemClick方法的position參數給出了自適應列表中所選項目的索引。所以,你可以在

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

發現原來項目的TAG_ID使適配器最後,讓你可以參考它在監聽器裏:

final ListAdapter adapter = ... 
+0

在那裏你的意思是寫/ – brabertaser19 2013-04-10 10:16:11

+0

becouse現在助手說我寫的getItem方法 – brabertaser19 2013-04-10 10:17:13

+0

在SimpleAdapter:http://developer.android.com/reference/android/widget/SimpleAdapter.html#getItem% 28int%29 – flup 2013-04-10 10:18:07

1

希望這有助於你。 你可以得到的ListSelectedItemIdPostionIntent通過,並得到它在SecondActivity現在這裏得到FirstActivityHashMap與定義它static

現在使用foreach循環擺脫HashMap所有值一樣

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(); 
    // ... 
} 
+0

現在與ListSelectedItemId的特定位置,你可以從currentList獲取值如何獲得它? – brabertaser19 2013-04-10 10:25:07

+0

如果你需要在sameActivity上,你可以在列表視圖ItemSelect方法使用上面的代碼 – 2013-04-10 10:25:58

+0

其他需要通過所有Hasmap SecondActivity – 2013-04-10 10:26:27

相關問題