如何从列表视图中获取一个参数

时间:2013-05-12 08:25:09

标签: android listview

我从json获取数据到列表视图

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);

            }

如果点击列表视图

,则执行操作
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();
            }
        }); 

我的问题如何从列表视图中获取id_user以获取是否单击并发送意图的id_user参数

3 个答案:

答案 0 :(得分:2)

当用户点击任何ListView行时,您可以从id_user HashMap获取followingList

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
    }
}

答案 1 :(得分:0)

您可以使用以下

在你的onItemClick(params)方法中

  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 

同时声明

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

在活动类

中的onCreate()之前

如果(followList.size()&gt; = position)在他的回答中提出了ρяσѕρєяK,也可以使用这个条件。要知道为什么条件是必要的,请在ρяσѕρєяK的答案中检查评论

同时检查以下链接并通过commonsware回答。使用Activity上下文代替getApplicationContext()

When to call activity context OR application context?

答案 2 :(得分:0)

您可以使用Collection类使用values()类的Collection来分派特定列表行的值。然后您可以将其转换为ArrayList。然后,您可以访问所点击列表行的每个值。

示例代码。

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);

我希望这会对你有所帮助。