免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2162 | 回复: 0
打印 上一主题 下一主题

Android中ListActivity的使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-18 21:50 |只看该作者 |倒序浏览
ListActivity使用
一个像数组或者是光标一样,通过绑定数据资源来陈列一系列选项的活动。当我们选择这些选项时,将会触发一个事件。
ListActivity主持操作着一个列表视图对象,这个列表视图能绑定不同的数据资源,典型的就是一个持有查询结果的
数组或者是光标。
屏幕布局(Screen Layout)
ListActivity有一个默认的布局,这个布局由单一的、全屏列表构成。我们通过在onCreate()中使用setContentView()
设置自己的视图布局来定制自己的屏幕布局。如果要完成这些,我们自己的视图必须包含一个id为“@android:id/list”
的ListView对象。如果我们的列表为空时,可以包含另外一个视图对象,这个空的列表必须有一个"android:empty"
值的id,注意到当有一个空的视图显示时,这个列表视图将会在没有任何数据时被隐藏。
排布局(Row Layout)
你可以在列表中确定单一的排,当然,你是通过在活动所操作的ListAdapter对象中设定的一 个资源布局来实现的。
一个ListAdapter持有一个的参数来为每一行确定了所使用到的需要布局的资源。同时,它还有另外两个参数,这两
个参数让你明确与哪个对象相互关联,通常是两个平行的数组。Android提供了一些标准的行布局资源。这些都是在
R.layout类中所定义的,名称像simple_list_item_1,simple_list_item_2,two_line_list_item之类一样。
下面我们来分析一段代码,这段代码就是使用了这个方面的知识:
    String songss[];
     File home = new File(MEDIA_PATH);//首先是创建一个File类型的实例对象home
        songss=home.list();    //列出目录文件中的所有文件并将这些文件名称放到字符串数组中
            ArrayAdapter songList = new ArrayAdapter(this,R.layout.song_item,songss);
//这是关键代码,创建一个ArrayAdapter对象,并将三个参数分别置为this(表明是当前上下文),
R.layout.song_item表明要在song_item中显示),songss(这就是显示在song_item中的内容),这样就将要显示在ListView中的内容设置好了。
            setListAdapter(songList);//使用setListAdapter(),就是使ArrayAdapter类型的songList运行,即真正起作用。
上面介绍了以上知识点ListView中ListAdapter的使用。
同时,Android不仅仅提供了数组,还提供了另外两个标准的列表适配器:处理static数据类型的SimpleAdapter和
处理光标结果查询的SimpleCursorAdapter.
下面是一个将名称和公司信息绑定到一个两行排布局的活动列表视图(所谓的两行排指的是两行数值作一个排布局,也
可以是三行作为一个排布局或者是一行作为一个行布局)
public class MyListAdapter extends ListActivity {      @Override     protected void onCreate(Bundle icicle){         super.onCreate(icicle);          // We'll define a custom screen layout here (the one shown above), but         // typically, you could just use the standard ListActivity layout.         setContentView(R.layout.custom_list_activity_view);          // Query for all people contacts using the Contacts.People convenience class.         // Put a managed wrapper around the retrieved cursor so we don't have to worry about         // requerying or closing it as the activity changes state.         mCursor = People.query(this.getContentResolver(), null);         startManagingCursor(mCursor);          // Now create a new list adapter bound to the cursor.          // SimpleListAdapter is designed for binding to a Cursor.         ListAdapter adapter = new SimpleCursorAdapter(                 this, // Context.                 android.R.layout.two_line_list_item,  // Specify the row template to use (here, two columns bound to the two retrieved cursor  rows).                 mCursor,                                    // Pass in the cursor to bind to.                 new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.                 new int[]);                                 // Parallel array of which template objects to bind to those columns.          // Bind to our new adapter.         setListAdapter(adapter);     } }


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/73521/showart_1728459.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP