如何从列表视图适配器位置对象值 [英] How to get object value from listview adapter position

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

问题描述

如何从适配器位置获得价值,我有code如下:

How to get value from adapter position, i have code in below:

CategoriesXmlParser categoryXmlParser = new CategoriesXmlParser();
            List<HashMap<String, Object>> categories = null;

            try {
                categories = categoryXmlParser.parse(reader);
            } catch (Exception e) {
                Log.d("Exception", e.toString());
            }
            String[] from = { "name", "image" };
            int[] to = { R.id.nama_kategori, R.id.logo_kategori };

            final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
                    categories, R.layout.per_item_kategori, from, to);
mListView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Object obj = mListView.getAdapter().getItem(position);
                    String value = obj.toString();
                    Log.d("MyLog", "Value is: "+value);
                    String name = // how code to get name value.
                }
            });

如果我看到登录它的logcat在MyLog我为得到:

If i look log it on logcat in the MyLog i get as:

值为:{位置= 12,
  IMAGE_PATH = HTTP://192.168.103.121/xml/icon.png,
  链接= HTTP://192.168.103.121/xml/kategori.php kat_id = 13,名称=驾驶员学校
  13}

Value is: {position=12, image_path=http://192.168.103.121/xml/icon.png, link=http://192.168.103.121/xml/kategori.php?kat_id=13, name=Kategori 13}

所以我的问题,我想从名称值,并存储到字符串变量的名字,我想在字符串名称得到的只是13驾驶员学校。因为我想将它传递给另一个活动。

So my question, I want to get value from name and stored to variable String name, I want to get just "Kategori 13" in String name. Because i want to passing it to another activity.

推荐答案

看起来你做与HashMap中的对象,例如blablabla.put(名称,值)?如是。试试这个:

Looks like you made ​​the object with hashmap, such blablabla.put("name", "value")?? If yes. Try this:

mListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        Object obj = mListView.getAdapter().getItem(position);
        String value = obj.toString();
        Log.d("MyLog", "Value is: "+value);
        String name = // how code to get name value.
    }
});

更改为:

mListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
            String name = (String) obj.get("name");
            Log.d("Yourtag", name);
    }
});

这篇关于如何从列表视图适配器位置对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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