Can Not Set or Get the ItemId of Listview

This is a discussion on Can Not Set or Get the ItemId of Listview within the Beginning Developers forums, part of the Android Development category; Hello all, on item click of listview I populated the following code Code: public class Ws extends Activity { Button btn; ArrayAdapter<String> aa; ArrayList<String> al; ...

Results 1 to 4 of 4

Thread: Can Not Set or Get the ItemId of Listview

  1. #1
    Android Lurker meral's Avatar
    Join Date
    Sep 2012
    Posts
    2

    Thumbs up Can Not Set or Get the ItemId of Listview

    Hello all,

    on item click of listview I populated the following code


    Code:
    public class Ws extends Activity  {
        Button btn;
        
        ArrayAdapter<String> aa;
        ArrayList<String> al;
        
        SimpleAdapter sa;
        ArrayList<HashMap<String, String>> mylist;
        
        ListView lv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ws);
             
             lv = (ListView) findViewById(R.id.listView1);
             
             mylist = new ArrayList<HashMap<String,String>>();
             
             al = new ArrayList<String>();
             
             HttpClient httpclient = new DefaultHttpClient();
             
             try {
                 String responseBody = httpclient.execute(new HttpPost("http://mydomain/userlist.php"),new BasicResponseHandler());
                 
                 JSONArray jArray = new JSONArray(responseBody);
                  for (int i = 0; i < jArray.length(); i++) {
                      JSONObject e = jArray.getJSONObject(i);
                     
                      //al.add(e.getString("name"));
                      // Toast.makeText(this, e.getString("name"), Toast.LENGTH_SHORT).show();
                      
                      HashMap<String, String> map = new HashMap<String, String>();
                      map.put("_id", e.getString("id"));
                      map.put("c1", e.getString("name"));
                      map.put("c2", e.getString("city"));
                      map.put("c3", e.getString("birthdate"));
    
                      
                      mylist.add(map); 
                  }
                  
               
                  String[] ColumnName = new String[] { "c1", "c2", "c3" };
                  int[] ColumnId = new int[] {R.id.colm1, R.id.colm2, R.id.colm3};
                  
                  sa = new SimpleAdapter(this, mylist, R.layout.custom_layout, ColumnName, ColumnId);
                  lv.setAdapter(sa);
                  
                  lv.setOnItemClickListener(new OnItemClickListener() {
    
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        
                        Toast.makeText(Ws.this, String.valueOf(arg2), Toast.LENGTH_SHORT).show();
                        Toast.makeText(Ws.this, String.valueOf(arg3), Toast.LENGTH_SHORT).show();
                    }
                });
                  
             } 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(); 
             }   
        }
    }
    But here both arg2 & arg3 giving me the same value that is position of list. But I need the id that I have set as _id in my mylist ArrayList

  2. Android.net is the premier Android Forum. Registered users do not see these ads. .

  3. #2
    Super Moderator cali's Avatar
    Join Date
    Sep 2011
    Posts
    4,830
    Thanked
    329 times
    Welcome to the site. Hope someone can help with this.

  4. #3
    Android Lurker meral's Avatar
    Join Date
    Sep 2012
    Posts
    2
    Hello, Noone is there to help me !!! Please....

  5. #4
    Android Lurker chakdev's Avatar
    Join Date
    Aug 2012
    Posts
    22
    Hi Meral,

    I hope you have already found a solution to your problem.
    To overcome your problem, you'll need to have a custom Adapter class.

    In the getView() method of your custom Adapter class, tag the view with your _id from myList.
    Something like this (Warning: pseudo-code. Most probably will not compile)

    Code:
    ArrayList<HashMap<String,String>> myList;
    
    public View getView(int position, View convertView, ViewGroup parent) {
           View newView= inflater.inflate(R.layout.custom_layout, null, false);
            newView.setTag(myList.get(position).get("_id"));
    }
    Then to retrieve _id in your onItemClick definition:

    Code:
    lv.setOnItemClickListener(new OnItemClickListener() {
    
    
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        
                       int _id = (int) arg1.getTag();
    
    
                        Toast.makeText(Ws.this, String.valueOf(arg2), Toast.LENGTH_SHORT).show();
                        Toast.makeText(Ws.this, String.valueOf(arg3), Toast.LENGTH_SHORT).show();
                    }
                });

Remove Ads

http://www.scramblerducati.org/

Sponsored Links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Similar Threads

  1. can not display listview data on Tablet device ?
    By hohoanganh205 in forum Beginning Developers
    Replies: 0
    Last Post: 07-13-2012, 04:22 AM
  2. Listview Help!
    By skewedToTheLeft in forum Beginning Developers
    Replies: 1
    Last Post: 05-21-2012, 04:17 AM
  3. Add a editview to a listview
    By shawrie in forum Beginning Developers
    Replies: 0
    Last Post: 09-21-2011, 02:43 PM
  4. bind webservice to listview
    By s.aijaz1 in forum Beginning Developers
    Replies: 2
    Last Post: 07-08-2011, 08:28 PM
  5. bind webservice to listview
    By s.aijaz1 in forum Introductions & Site Assistance
    Replies: 1
    Last Post: 07-04-2011, 09:04 PM

Search tags for this page

android get id of item
,
android get listview item editview
,
android list set item id
,
android listview get item.id
,
android listview get value
,
android listview item editview
,
android listview set itemid
,

android set id on listview

,
arrayadapted itemid android
,
get listview id from listview item android
,
listview.get(arg2).get(id);
,
public void onitemclick(adapterview arg0, view arg1, int arg2, long itemid) {
,
put 10 names in a arraylist android
,
vs.net listview get itemid from name
Click on a term to search our sites for related topics.
Android Forum