android - 如何从 ListView 中获取一个参数

标签 android listview

我从json获取数据到 ListView

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

            }

点击 ListView 时的 Action

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

我的问题是如何从 ListView 中获取 id_user 以获取是否单击并发送 id_user 参数以获取 Intent

最佳答案

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

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

关于android - 如何从 ListView 中获取一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16505499/

相关文章:

java - Android - 媒体播放器 : How to separating fastForward to another method (that later will be call)?

c# - 替换 ListView 中列下的项目文本

Android - ListView 项目点击显示数据到新 Activity

java - BaseExpandableListAdapter 中的不可扩展项目

android - 两个ListView上面的TextView

android - DialogFragment 不显示

android - 如何获得 X 按钮以停止 Android P 上的媒体样式通知?

android - 相机自动对焦不适用于 Galaxy S4

android - 如何使用 Universal Image Loader Android 强制清除缓存?

android - ArrayAdapter 要求 ID 为 TextView 错误