Home
JavaEE
Java GUI
Framework
DHtml
Android
插画
留言本
  • 在android的sdk中动态添加view的方法
  • 我和老婆的婚纱相,自已做成了web方式
  • Java下实现XP强制关机的方法
  • 对暴力破解带密码的QQ相册的可行性分析
  • 今晚在163上看到新闻,但最令我感概的是该项新闻的评论
  • 我用android sdk做的一个扫雷游戏
  • 看看android扫雷游戏的主要代码
  • 哈哈,老婆怀上了!婚礼不变,还是在正月十二!
  • Android中,Drawable接口及其子类的使用方法
  • 必须转载的一篇非常感人的文章!
  • 在客户端IE中使用jasperView,客户端显示JasperViewer
  • jspsmartupload取得参数的值是乱码的问题
  • 客户端显示JasperViewer并在Jasperview的applet中加超链接
  • Hibernate基于链接表的多对多many-to-many双向关联例子
  • Hibernate双主键的设置例子
  • Hibernate中的properties和formula的用法
  • Android中,Drawable接口及其子类的使用方法
  • Android中用Bitmap获取图片中某个区域的图象
  • Android中AnimationDrawable实现简单动画的例子
  • Animation与Interpolator及AnimationSet的用法
Android中给Spinner或ListView添加数据
posted by David Chen at May 6, 2009, 5:22 PM

先看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>
Labels:   Android  
Trackback:   http://cwq.iou1314.com/android-spinner-listview_a349
本站内的任何文章,只代表个人意见或学习所用,如有版权声明,请尊重作者的劳动成果,在转载时请保留原始链接并注明出处。
© 2007 - 2010 CWQ.IOU1314.COM All Rights Reserved.