Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1013940
  • 博文数量: 123
  • 博客积分: 5051
  • 博客等级: 大校
  • 技术积分: 1356
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-14 10:56
文章分类
文章存档

2012年(1)

2011年(21)

2010年(13)

2009年(55)

2008年(33)

分类: Java

2008-12-18 21:50:31

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);
     }
 }

 
阅读(15649) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-08-10 10:39:28

楼主辛苦了, 不过翻译的实在差强人意。 请楼主注意提高英语先。

chinaunix网友2009-05-15 22:49:06

People.query(this.getContentResolver(), null),关于这个方法,好像没有吧?

chinaunix网友2009-05-15 22:47:49

People.query(this.getContentResolver(), null),关于这个访法楼主,你能调用到?