如何从列表视图中获取一个参数 [英] How to get one parameter from list view

查看:104
本文介绍了如何从列表视图中获取一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

您可以使用以下

在您的onItemClick(params)方法中

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 

也声明

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

在活动类内的onCreate()之前

before onCreate() inside the activity class

也可以按照if(followingList.size()> = position)的条件使用此条件,如ρяσsρєяK在他的答案中建议的那样.要知道为什么需要这种条件,请查看ρяσsρєяK的答案中的评论

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

也请检查下面的链接,并通过通用软件进行回答.使用活动上下文代替getApplicationContext()

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

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

这篇关于如何从列表视图中获取一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆