1

我从 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 参数以表示意图

4

3 回答 3

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
    }
}
于 2013-05-12T08:31:49.583 回答
0

您可以使用Collectionclass 来调度特定列表行的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);

我希望这能帮到您。

于 2013-05-12T08:50:06.533 回答
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() 之前

也使用这个条件 if(followingList.size()>=position) 作为 ρяσѕρєя K 在他的回答中建议。要知道为什么这个条件是必要的,请检查 ρяσѕρєя K 的答案中的评论

还要检查下面的链接并通过 commonsware 回答。使用 Activity 上下文代替 getApplicationContext()

何时调用活动上下文或应用程序上下文?

于 2013-05-12T08:32:07.207 回答