Android ArrayAdapter 详解
来源:互联网 发布:ubuntu win10启动项 编辑:程序博客网 时间:2024/05/01 16:21
本文主要讲解ArrayAdapter
的创建方法,我把ArrayAdapter
分为三种:简单的、样式丰富的但内容简单的、内容丰富的。默认情况下,ArrayAdapter期望接受的样式文件里只含有一个textview
,然后它把接受到的数据toString
后(即调用数据对象的toString
方法)展示在textview
里。
简单的
这样的列表的每一行都只有一行文字。
// 当然listview 也可以是在layout里写好,然后findViewById()获取出来,这样的话后面就不需setContentView(listview); ListView listview = new ListView(this); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1); adapter.add("string1"); adapter.add("haha"); adapter.add("heihei"); listview.setAdapter(adapter); setContentView(listview);
上面代码中,android.R.layout.simple_expandable_list_item_1
是android
里已提供的样式,我们也可换成自己的xml
。但是需要注意的是这个xml
文件仅能有一个textview
。连Layout
也不能有。否则会报错:ArrayAdapter requires the resource ID to be a TextView
如layout
下有online_user_list_item.xml
,它的内容如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/online_user_list_item_textview" > </TextView>
则android.R.layout.simple_expandable_list_item_1
换成R.layout.online_user_list_item
。
如果我们想要使用更复杂一点的layout
,而不仅是只有一个textview
,那就要用下面这种。
样式丰富但内容简单的。
layout
下的online_user_list_item.xml
内容如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/online_user_list_item_textview" android:text="TextView"></TextView> <Button android:text="button" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> </LinearLayout>
里面含有的textview
是我们想要展示内容的地方。那么构建ArrayAdapter
时,应该这样写:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.online_user_list_item, R.id.online_user_list_item_textview);
如果我们需要展示的内容是一仅一个textview
承载不了的,还需要其它组件,怎么办?我们可以自定义。
内容丰富的(自定义ArrayAdapter)。
这就需要写一个类继承自ArrayAdapter
并且重写getView
方法。上代码:
public class UserListAdapter extends ArrayAdapter<User> { private int resourceId; public UserListAdapter(Context context, int textViewResourceId, List<User> objects) { super(context, textViewResourceId, objects); this.resourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent){ User user = getItem(position); LinearLayout userListItem = new LinearLayout(getContext()); String inflater = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater); vi.inflate(resourceId, userListItem, true); TextView tvUsername = (TextView)userListItem.findViewById(R.id.tv_user_list_username); TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.tv_user_list_askednum); TextView tvLastMsg = (TextView)userListItem.findViewById(R.id.tv_user_list_lastmsg); tvUsername.setText(user.getUsername()); tvAskedNum.setText(String.valueOf(user.getAskedNum())); tvLastMsg.setText(user.getLastMsg()); return userListItem; } }
activity
里就这样写:
List<User> users = new ArrayList<User>(); User user = new User(); user.setAskedNum(8); user.setLastMsg("hello"); user.setUsername("pxx"); users.add(user); users.add(user); users.add(user); UserListAdapter adapter = new UserListAdapter(this,R.layout.online_user_list_item,users); listview.setAdapter(adapter);
转自
- http://daimajishu.iteye.com/blog/1088680
- Android ArrayAdapter 详解
- Android ArrayAdapter 详解
- Android ArrayAdapter 详解
- Android ArrayAdapter 详解
- Android ArrayAdapter 详解 .
- android ArrayAdapter详解
- Android ArrayAdapter 详解
- Android ArrayAdapter 详解
- Android ArrayAdapter 详解
- Android ArrayAdapter 详解
- Android ArrayAdapter 详解
- Android ArrayAdapter 详解
- Android ArrayAdapter 详解
- Android之ArrayAdapter详解
- Android之ListView详解ArrayAdapter
- Android ListView && ArrayAdapter开发详解
- Android之ArrayAdapter的详解
- Android中SimpleAdapter,ArrayAdapter和BaseAdapter详解
- Unity3D学习笔记《Space Shooter》二
- mezzanine timezone 的问题
- socket通信之八:完成端口模型实现的客户/服务器模型
- partial关键字
- 01-01 Creational Patterns
- Android ArrayAdapter 详解
- Linux内核中trace_xxxx()函数的定义
- POJ2309 BST(树状数组)
- 欢迎来挑战:极限打印99乘法表
- 九度OJ-题目1003:A+B
- HDU5003-Osu!-AsiaRegionalAnshanOnline2014
- 修改view的x,y,width,height值的方法
- Unable to locate the model you have specified
- 从沙盒中取出被保存的图片