您的位置:首页 > 移动开发 > Android开发

Android中给Spinner或ListView添加数据

2010-12-10 08:58 309 查看
标签:

android

listview

it

分类: android学习笔记
先看ListView的api文档的说明
A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.
ListView是一个垂直显示选项的滚动列表,它的数据来自ListAdapter。注意它是可以多选,多显示的。

再看Spinner的说明
A view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view.
Spinner只显示并只能选择一个选项的,它的数据来自Adapter。

就是说两者都要通过Adapter来获得数据并显出来。做法都差不多,看如下的代码,可以生成一个下拉框。
public class Select extends Spinner {

ArrayAdapter arr; // 存放数据的数组Adapter,是impl自Adapter的一种

public Select(Context context) {
super(context);
// TODO Auto-generated constructor stub

String[] strs = new String[] {
context.getString(R.string.easy),
context.getString(R.string.normal),
context.getString(R.string.hard) };

// 如果Spinner,ListView中放的内容 是TextView的,必须在res/layout中添加一个xml,
// 且该xml中的内容只能是TextView的xml,以该xml中的TextView做模生成item
arr = new ArrayAdapter(context, R.layout.list_item, strs);
this.setAdapter( arr );
// 通过这样,可以在Spinner中显示3个TextView类型的选项。
}

}


list_item.xml, 该xml中的textView将作为模板使用,可以在这里指定文字的颜色,大小等。
<?xml version="1.0" encoding="UTF-8"?>
<TextView ...>
</TextView>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: