`

Android学习笔记(5)-关于ListActivity的简单体验

阅读更多
今天学习点轻松的内容吧,看看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);       
<        List<String> items = fillArray();       
<        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_row,items);
<              
<        this.setListAdapter(adapter);
<    }
    private List<String> fillArray()    ...{
<        List<String> items = new ArrayList<String>();
<        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 List<HashMap<String, String>> fillMaps()
    ...{
<        List<HashMap<String, String>> items = new ArrayList<HashMap<String,String>>();
<      
<        HashMap<String,String> i = new HashMap<String,String>();
<        i.put("name","日曜日");
<        i.put("key", "SUN");
<        items.add(i);
<        HashMap<String,String> i1 = new HashMap<String,String>();
<        i1.put("name","月曜日");
<        i1.put("key", "MON");
<        items.add(i1);
<        HashMap<String,String> i2 = new HashMap<String,String>();
<        i2.put("name","火曜日"); 
<        i2.put("key", "TUE");
<        items.add(i2);
<        HashMap<String,String> i3 = new HashMap<String,String>();
<        i3.put("name","水曜日");
<        i3.put("key", "WED");
<        items.add(i3);
<        HashMap<String,String> i4= new HashMap<String,String>();
<        i4.put("name","木曜日");
<        i4.put("key", "THU");
<        items.add(i4);
<        HashMap<String,String> i5 = new HashMap<String,String>();
<        i5.put("name","金曜日");
<        i5.put("key", "FRI");
<        items.add(i5);
<        HashMap<String,String> i6 = new HashMap<String,String>();
<        i6.put("name","土曜日");
<        i.put("key", "SAT");
<        items.add(i6);
<
<        return items;
<    }
然后,在HelloTwoB中的onCreate函数中,修改代码,有几个不同:items的元素是HashMap实例,这是一点变化,然后构造函数除了要求items以外,还要求提供一个string[]来说明用hash表中的哪个字段显示在列表中,而后是一个资源ID的数组。我的代码是这样的:
<//SimpleAdapter demo
<List<HashMap<String, 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">
< ... ...
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics