Android中给Spinner或ListView添加数据
(2009-07-17 14:30:52)
标签:
android
listview
it
先看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将作为模板使用,可以在这里指定文字的颜色,大小等。
encoding="UTF-8"?>
分享:
喜欢
0
赠金笔
加载中,请稍候......
评论加载中,请稍候...
发评论
登录名: 密码: 找回密码 注册记住登录状态
昵 称:
评论并转载此博文
发评论
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。