2012-07-12 6 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

+0

여기서'id'에 대한 값을 설정하고 있습니까? –

+0

어댑터 코드를 알려주십시오. – AMerle

답변

2

:

항상 년대 last..you 배열과 같은에 저장한다 그 이유는
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의 가치를 재정의 할 때마다 ..

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

어떻게해야합니까 ?? 당신은 정교하게 만들어 주길 바래 .. –

+0

내 대답을 업데이 트 .. – Nermeen

+0

고마워 내 대답을 ..... ..... –

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); 
     } 
    }); 
관련 문제