1

i get data from json to list view

for (int i = 0; i < following.length(); i++) {
                JSONObject c = following.getJSONObject(i);

                // Storing each json item in variable
                String nama = c.getString(KEY_NAMA);
                String instansi = c.getString(KEY_INSTANSI);
                String status = c.getString(KEY_STATUS);
                id_user = c.getString(KEY_ID_USER);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(KEY_NAMA, nama);
                map.put(KEY_INSTANSI, instansi);
                map.put(KEY_STATUS, status);
                map.put(KEY_ID_USER, id_user);
                // adding HashList to ArrayList
                followingList.add(map);

            }

and action if listview on click

list = (ListView) activity.findViewById(R.id.listView1);
        // Getting adapter by passing xml data ArrayList
        adapter1 = new LazyAdapter(activity, followingList);
        list.setAdapter(adapter1);

        list.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent profil = new Intent(activity.getApplicationContext(),
                        ProfilFollower.class);
                profil.putExtra("id_user", id_user);
                profil.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                activity.startActivity(profil);
                // Closing dashboard screen
                activity.finish();
            }
        }); 

My question how to get id_user from list view to get if on click and send id_user parameter for intent

Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

2

you can get id_user from followingList HashMap when user clicked on any ListView row as:

public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    if(followingList.size()>0){
      HashMap<String, String> selected_user_info=followingList.get(position);
      String str_user_id=selected_user_info.get(KEY_ID_USER);
      //... do your work here
    }
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • can u explain why this if(followingList.size()>=position) is necessary. array index start form 0 and list position from 1? – Raghunandan May 12 '13 at 08:44
  • 1
    @Raghunandan : you are right but if ListView also have headerView then first postion is 1 otherwise 0. i have just included if condtion to avoid `arrayindexoutofboundsexception` in case any how followingList List get Cleaned – ρяσѕρєя K May 12 '13 at 08:49
  • got it thanks for the clarification.+1 for the clarification and the answer – Raghunandan May 12 '13 at 08:49
0

You can use the below

In your onItemClick(params) methods

  Intent profil = new Intent(ActivityName.this,
                   ProfilFollower.class);
  profil.putExtra("id_user",      
  followingList.get(position).map.get(KEY_ID_USER).toString());  
  //map.get(KEY_ID_USER) where KEY_ID_USER is the key 

Also declare

     HashMap<String, String> map = new HashMap<String, String>();

before onCreate() inside the activity class

Also use this condiditon if(followingList.size()>=position) as ρяσѕρєя K suggested in his answer. To know why the condition is necessary check the comment's in ρяσѕρєя K's answer

Also check the link below and answer by commonsware. Use Activity context in place of getApplicationContext()

When to call activity context OR application context?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

You can use Collection class to dispatch the values of the particular list row using values() of Collection class. And then you can convert it as ArrayList. Then you can access each value of your clicked list row.

Example code.

Collection<Object> str = followingList.get(position).values();
ArrayList<Object> al1 = new ArrayList<Object>(str);
String idUser = al1.get(id_user).toString(); // if your id_user has global access. 
//Otherwise you could use something al1.get(0).toString() like this. 
Intent profil = new Intent(ActivityName.this,
                    ProfilFollower.class);
profil.putExtra("id_user", idUser);

I hope this will help you.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128