在android开发替换SimpleAdapter改善code质量 [英] Replace SimpleAdapter in android development to improve code quality

查看:132
本文介绍了在android开发替换SimpleAdapter改善code质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我用的是的HashMap<字符串,字符串> SimpleAdapter 对象来显示用户数据在我的的ListView ,但现在我想更换的HashMap<字符串,字符串> 的ArrayList<使用者>(); 含实际用户对象

At the moment, I use a HashMap<String, String> and a SimpleAdapter object to display user data in my ListView, but now I want to replace the HashMap<String, String> with an ArrayList<User>(); containing real user objects.

下面是我的code:

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

                map.put(TAG_USERNAME, c.getString(TAG_USERNAME));
                map.put(TAG_FIRSTNAME, c.getString(TAG_FIRSTNAME));
                map.put(TAG_LASTNAME, c.getString(TAG_LASTNAME));
                map.put(TAG_ADDRESS, c.getString(TAG_ADDRESS));
                usersList.add(map);

[...]

ListAdapter adapter = new SimpleAdapter(this, usersList,
                    R.layout.single_user, new String[] { TAG_USERNAME,
                            TAG_FIRSTNAME, TAG_LASTNAME, TAG_ADDRESS },
                    new int[] { R.id.textViewUsername, R.id.textViewFirstName,
                            R.id.textViewLastName, R.id.textViewAddress });

这怎么可能与的ArrayList 替换的HashMap ?是否有一个特定的类用于此目的?

How is it possible to replace the HashMap with the ArrayList? Is there a specific class for this purpose?

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

推荐答案

最简单的方法是将覆盖的toString 为了回报您想要的信息,您的用户类中显示,并使用 ArrayAdapter&lt;使用者&GT; 。或者你可以有你的 CustomAdapter ,它扩展了 BaseAdapter 并实现其抽象方法。在particulare你有 getCount将方法,该方法返回一个数据集中持有,的getItem(INT位置)元素的数量返回数据集对象的位置,而 getView(INT,查看,ViewGroup中),你必须使用膨胀和自定义视图

the easiest way would be to override toString inside your User class in order to return the information you want to display, and use an ArrayAdapter<User>. Or you can have your CustomAdapter, which extends BaseAdapter and implements its abstract method. In particulare you have the getCount method, which returns the number of elements that your dataset holds, getItem(int position) which returns the object of your dataset at position, and getView(int, View, ViewGroup), that you have to use to inflate and customize your view

这篇关于在android开发替换SimpleAdapter改善code质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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