2012-07-12 112 views
0

我想將listview中單擊的項目的ID傳遞給下一個活動,但每當我點擊該項目時,它總是獲取最後一個項目的ID該listview這裏是我的代碼Android:無法獲取列表視圖中項目的ID

String src = a.getText().toString(); 

      ListView srceve = (ListView)findViewById(R.id.list_scr_planner); 
      ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 
      if(src.length()>0) 
      try{ 

      HttpGet post=new HttpGet("xxxxxxxxxxxxxxxxxxxxxxxxxxxx"); 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
      nameValuePairs.add(new BasicNameValuePair("eventname", src)); 
      HttpResponse resp = login.client.execute(post); 
      String re = EntityUtils.toString(resp.getEntity()); 

      Log.i("loggg", "error" + re); 


      re= "{\"root\": " + re + "}"; 
      JSONObject root = new JSONObject(re); 
      JSONArray sessions = root.getJSONArray("root"); 
      Log.i("loggg", "error" + sessions); 
      for(int i=0;i<sessions.length();i++){ 
       HashMap<String, String> map2 = new HashMap<String, String>(); 
       JSONObject e = sessions.getJSONObject(i); 
       map2.put("event_name", e.getString("name")); 
       map2.put("event_date", e.getString("eventdate")); 
       mylist.add(map2); 
       id = e.getString("id"); 

      } 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     SimpleAdapter adapt= new SimpleAdapter(searchevent.this,mylist, R.layout.list_custom ,new String[]{"event_name","event_date"},new int []{R.id.textnam,R.id.textdate}); 
     srceve.setAdapter(adapt); 

     srceve.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View arg1, 
        int pos, long arg3) { 
       // TODO Auto-generated method stub 
       parent.getItemAtPosition(pos); 
       Toast.makeText(getApplicationContext(), ""+ id + evename, 0).show(); 
       Intent i = new Intent(searchevent.this, eventDetails.class); 
       i.putExtra("ar", id); 
       startActivity(i); 
      } 
     }); 

,請告訴我,我錯了,我取回這是來自服務器的項目的ID。感謝

+0

試試這個,i.putExtra(「ar」,pos); – Aerrow 2012-07-12 10:03:07

+0

你在哪裏設置'id'的值? – 2012-07-12 10:04:13

+0

請提供我們的適配器代碼。 – AMerle 2012-07-12 10:04:35

回答

2

在循環:

for(int i=0;i<sessions.length();i++){ 
      HashMap<String, String> map2 = new HashMap<String, String>(); 
      JSONObject e = sessions.getJSONObject(i); 
      map2.put("event_name", e.getString("name")); 
      map2.put("event_date", e.getString("eventdate")); 
      mylist.add(map2); 
      id = e.getString("id"); 

     } 

每次要覆蓋的id價值的時候,這就是爲什麼它總是last..you應該將其保存在一個數組一樣..

String[] ids = new String[sessions.length()]; 
for(int i=0;i<sessions.length();i++){ 
      HashMap<String, String> map2 = new HashMap<String, String>(); 
      JSONObject e = sessions.getJSONObject(i); 
      map2.put("event_name", e.getString("name")); 
      map2.put("event_date", e.getString("eventdate")); 
      mylist.add(map2); 
      ids[i] = e.getString("id"); 

     } 

然後在onItemClick,根據所選項目的位置得到它:

public void onItemClick(AdapterView<?> parent, View arg1, 
       int pos, long arg3) { 
      // TODO Auto-generated method stub 
      parent.getItemAtPosition(pos); 

      String id = ids[pos]; 

      Toast.makeText(getApplicationContext(), ""+ id + evename, 0).show(); 
      Intent i = new Intent(searchevent.this, eventDetails.class); 
      i.putExtra("ar", id); 
      startActivity(i); 
     } 
+0

如何做?請你詳細說明.. – 2012-07-12 10:21:14

+0

更新了我的答案.. – Nermeen 2012-07-12 10:25:20

+0

感謝這麼多得到了我的答案..... :) – 2012-07-12 10:34:41

0
srceve.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View arg1, 
       int pos, long arg3) { 
     // TODO Auto-generated method stub 
     String id=parent.getId();//this will give you the id of the selected item View 
     parent.getItemAtPosition(pos); 
     Toast.makeText(getApplicationContext(), ""+ id + evename, 0).show(); 
     Intent i = new Intent(searchevent.this, eventDetails.class); 
     i.putExtra("ar", id); 
     startActivity(i); 
     } 
    }); 
相關問題