免费注册 查看新帖 |

Chinaunix

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

Android学习笔记(5)-关于ListActivity的简单体验 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-18 11:04 |只看该作者 |倒序浏览
今天学习点轻松的内容吧,看看android.app包里的几个类。首先是这个在平台自的例子中被广泛使用的ListActivity。这个类其实就是一个含有一个ListView组件的Activity类。也就是说,如果我们直接在一个普通的Activity中自己加一个ListView也是完全可以取代这个ListActivity的,只是它更方便而已,方便到什么程度呢?来做个例子瞧瞧。

public class HelloTwoB extends ListActivity


...{   


    public void onCreate(Bundle icicle) ...{

        super.onCreate(icicle);

        setTheme(android.R.style.Theme_Dark);

        setContentView(R.layout.mainb);

        

        ListString> items = fillArray();        

        ArrayAdapterString> adapter = new ArrayAdapterString>(this,R.layout.list_row,items);

               

        this.setListAdapter(adapter);

    }

    private ListString> fillArray()


    ...{

        ListString> items = new ArrayListString>();

        items.add("日曜日");

        items.add("月曜日");

        items.add("火曜日");

        items.add("水曜日");

        items.add("木曜日");

        items.add("金曜日");

        items.add("土曜日");

        return items;

    }

   

    @Override

    protected void onListItemClick(ListView l, View v, int position, long id)


    ...{

        TextView txt = (TextView)this.findViewById(R.id.text);

        txt.setText("あすは "+l.getSelectedItem().toString()+"です。");

    }

}
的确可以简单到只需准备一个List对象并借助Adapter就可以构造出一个列表。重载onListItemClick方法可以响应选择事件,利用第一个参数可以访问到这个ListView实例以得到选中的条目信息。这里有一点要说明的,就是如果更简单的话,其实连那个 setContentView都可以不要了,Android也会自动帮我们构造出一个全屏的列表。但是本例中我们需要一个TextView来显示选中的条目,所以我们需要一个layout.mainb描述一下这个列表窗口。

xml version="1.0" encoding="utf-8"?>

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

TextView id="@+id/text"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text=""

    />

ListView id="@id/android:list"

    android:layout_width="fill_parent"

    android:layout_height="0dip"

    android:layout_weight="1"

    android:drawSelectorOnTop="false"

    />

LinearLayout>
这里需要注意的是那个ListView的ID,是系统自定义的android:list,不是我们随便取的,否则系统会说找不到它想要的listview了。然后,在这个listview之外,我们又增加了一个TextView,用来显示选中的条目。
再来说说这里用到的ArrayAdapter,它的构造函数中第二个参数是一个资源ID,ArrayAdapter的API文档中说是要求用一个包含 TextView的layout文件,平台用它来显示每个选择条目的样式,这里的取值是R.layout.list_row,所以,我们还有一个 list_row.xml文件来描述这个布局,相当简单。

xml version="1.0" encoding="utf-8"?>

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

TextView id="@+id/item"

          xmlns:android="http://schemas.android.com/apk/res/android"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"/>

TextView id="@+id/item2"

          xmlns:android="http://schemas.android.com/apk/res/android"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"/>

LinearLayout>
从ArrayAdapter上溯到BaseAdapter,发现还有几个同源的Adapter也应该可以使用,象SimpleAdapter和CursorAdapter,还是做个例子来实验一下吧。
先看看SimpleAdapter,说是simple却不simple。
首先看看这个fillMaps方法,基本上就明白这个simpleAdapter是怎么回事了,在有些场合它还是挺有用的,可以为每个条目绑定一个值:

private ListHashMapString, String>> fillMaps()


    ...{

        ListHashMapString, String>> items = new ArrayListHashMapString,String>>();

      

        HashMapString,String> i = new HashMapString,String>();

        i.put("name","日曜日");

        i.put("key", "SUN");

        items.add(i);

        HashMapString,String> i1 = new HashMapString,String>();

        i1.put("name","月曜日");

        i1.put("key", "MON");

        items.add(i1);

        HashMapString,String> i2 = new HashMapString,String>();

        i2.put("name","火曜日");  

        i2.put("key", "TUE");

        items.add(i2);

        HashMapString,String> i3 = new HashMapString,String>();

        i3.put("name","水曜日");

        i3.put("key", "WED");

        items.add(i3);

        HashMapString,String> i4= new HashMapString,String>();

        i4.put("name","木曜日");

        i4.put("key", "THU");

        items.add(i4);

        HashMapString,String> i5 = new HashMapString,String>();

        i5.put("name","金曜日");

        i5.put("key", "FRI");

        items.add(i5);

        HashMapString,String> i6 = new HashMapString,String>();

        i6.put("name","土曜日");

        i.put("key", "SAT");

        items.add(i6);


        return items;

    }
然后,在HelloTwoB中的onCreate函数中,修改代码,有几个不同:items的元素是HashMap实例,这是一点变化,然后构造函数除了要求items以外,还要求提供一个string[]来说明用hash表中的哪个字段显示在列表中,而后是一个资源ID的数组。我的代码是这样的:

//SimpleAdapter demo

ListHashMapString, String>> items = fillMaps();


SimpleAdapter adapter=new SimpleAdapter(this,items,R.layout.list_row,new String[]...{"name"},new int[]...{R.id.item});
编译跑一下可以看到结果了,是吧?只是显示的文字不太对,再改一下:

protected void onListItemClick(ListView l, View v, int position, long id)


    ...{

        TextView txt = (TextView)this.findViewById(R.id.text);

        txt.setText("あすは "+((HashMap)l.obtainItem(position)).get("key").toString()+"です。");

    }
这样就好多了,其实一般情况下我们都是用ListView中的obtainItem取得当前选中的条目,然后转成List中的对应类型来使用的。
上面的例子中只显示name对应的值,其实你也可以试一下这样:


SimpleAdapter adapter=new SimpleAdapter(this,items,R.layout.list_row,new String[]...{"name","key"},new int[]...{R.id.item,R.id.item2});
看看是什么效果。
再看看那个CursorAdapter吧,它的列表中元素要求是Cursor,这东西与DB有关,不过最简单的DB就是通讯簿。先从Contacts.People入手吧,同样修改代码:

//CursorAdapter demo

Cursor mCursor = this.getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, null);


SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.list_row,mCursor,new String[]...{Contacts.People.NAME},new int[]...{R.id.item});
因为单纯的CursorAdapter是抽象类,所以我用的是它的子类SimpleCursorAdapter,很好理解,先用 ContentResolver查询通讯簿得到一个游标,然后告诉SimpleCursorAdapter要用其中的People.NAME作为显示项来构造出一个adapter即可。
现在的onListItemClick也不一样了,如下:

    protected void onListItemClick(ListView l, View v, int position, long id)


    ...{

        TextView txt = (TextView)this.findViewById(R.id.text);

        Cursor c = (Cursor)l.obtainItem(position);

        txt.setText("SEL = "+c.getString(c.getColumnIndex(Contacts.People.NUMBER)));

    }        
这里同样是先用obtainItem取到游标,然后用从记录中取出想要的字段显示即可。在做这个例子时,因为权限的问题我们还得修改一下AndroidManifest.xml文件,让我们的应用可以访问到通讯簿:

manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="cn.sharetop.android.hello.two">

    uses-permission id="android.permission.READ_CONTACTS" />

    application android:icon="@drawable/icon">

... ...
OK,先到这里,CSDN今天相当不稳定,不知道能否发布成功。

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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP