导航

ListActivity and viewgroup

Posted on 2011-03-18 23:45  NCUT蓝色理想  阅读(444)  评论(0编辑  收藏  举报
Android允许为列表中一个单独的行指定布局。只要在ListAdapter对象中指定一个布局资源就可以了。

一个ListAdapter构造函数有一个参数来指定每一行的布局资源。此外,它还有另外两个参数来指定哪一个数据域与行布局资源中的对象相关联。这两个参数一般是平行数组。

Android在R.layout类中提供了一些标准的布局资源。例如simple_list_item_1, simple_list_item_2, 和two_line_list_item。

参考示例一(使用SimpleCursorAdapter):

1. 使用默认的布局。

2. Activity对应的Java代码如下。

Java代码package com.xeedroid;

import android.app.ListActivity;

import android.database.Cursor;

import android.os.Bundle;

import android.provider.ContactsContract.Contacts;

import android.widget.ListAdapter;

import android.widget.SimpleCursorAdapter;

public class ListActivityDemo extends ListActivity {

        protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

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

                                null, null, null, null);

                startManagingCursor(mCursor);

                ListAdapter adapter = new SimpleCursorAdapter(this,

                                android.R.layout.two_line_list_item, mCursor, new String[] {

                                                Contacts.DISPLAY_NAME, Contacts.TIMES_CONTACTED }, new int[] {

                                                android.R.id.text1, android.R.id.text2 });

                setListAdapter(adapter);

        }

}

复制代码客户化的行布局使用了系统的android.R.layout.two_line_list_item。

参考示例二(使用ResourceCursorAdapter):

1. 使用系统默认screen layout。

2. Activity对应的Java代码QuickContactsDemo.java(带注释)如下:

Java代码package com.example.android.apis.app;

import com.example.android.apis.R;

import android.app.ListActivity;

import android.content.Context;

import android.database.CharArrayBuffer;

import android.database.Cursor;

import android.os.Bundle;

import android.provider.ContactsContract.Contacts;

import android.view.View;

import android.view.ViewGroup;

import android.widget.QuickContactBadge;

import android.widget.ResourceCursorAdapter;

import android.widget.TextView;

public class QuickContactsDemo extends ListActivity {

    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {

            Contacts._ID, // 0

            Contacts.DISPLAY_NAME, // 1

            Contacts.STARRED, // 2

            Contacts.TIMES_CONTACTED, // 3

            Contacts.CONTACT_PRESENCE, // 4

            Contacts.PHOTO_ID, // 5

            Contacts.LOOKUP_KEY, // 6

            Contacts.HAS_PHONE_NUMBER, // 7

    };

    static final int SUMMARY_ID_COLUMN_INDEX = 0;

    static final int SUMMARY_NAME_COLUMN_INDEX = 1;

    static final int SUMMARY_STARRED_COLUMN_INDEX = 2;

    static final int SUMMARY_TIMES_CONTACTED_COLUMN_INDEX = 3;

    static final int SUMMARY_PRESENCE_STATUS_COLUMN_INDEX = 4;

    static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 5;

    static final int SUMMARY_LOOKUP_KEY = 6;

    static final int SUMMARY_HAS_PHONE_COLUMN_INDEX = 7;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // 查找符合条件的所有contact

        String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("

                + Contacts.HAS_PHONE_NUMBER + "=1) AND ("

                + Contacts.DISPLAY_NAME + " != '' ))";

        Cursor c =

                getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,

                null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

        // 将cursor交给Activity管理

        startManagingCursor(c);

        // 创建adapter,将客户化的UI和要显示的数据与adapter绑定

        ContactListItemAdapter adapter = new ContactListItemAdapter(this, R.layout.quick_contacts, c);

        // 将adapter和当前list activity绑定

        setListAdapter(adapter);

    }

        /*

        * ResourceCursorAdapter主要是将数据按照ListActivity的要求传递给它。它的祖宗类实现了List Adapter接口。

        * 在对ListActivity界面进行渲染过程中,对于Cursor中每一条记录都会依次调用newView和bindView方法来生成UI。

        */

    private final class ContactListItemAdapter extends ResourceCursorAdapter {

        public ContactListItemAdapter(Context context, int layout, Cursor c) {

            super(context, layout, c);

        }

        // 将newView生成的view和当前cursor指定的数据绑定

        @Override

        public void bindView(View view, Context context, Cursor cursor) {

            final ContactListItemCache cache = (ContactListItemCache) view.getTag();

            TextView nameView = cache.nameView;

            QuickContactBadge photoView = cache.photoView;

            // Set the name

            cursor.copyStringToBuffer(SUMMARY_NAME_COLUMN_INDEX, cache.nameBuffer);

            int size = cache.nameBuffer.sizeCopied;

            cache.nameView.setText(cache.nameBuffer.data, 0, size);

            final long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);

            final String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY);

            cache.photoView.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));

        }

    class ListItemView extends ViewGroup{

        // 按照ContactListItemAdapter(Context context, int layout, Cursor c)中的layout生成view

        @Override

        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            View view = super.newView(context, cursor, parent);

            ContactListItemCache cache = new ContactListItemCache();

            cache.nameView = (TextView) view.findViewById(R.id.name);

            cache.photoView = (QuickContactBadge) view.findViewById(R.id.badge);

            // Tag用于传递任意对象,将当前方法生成的view中的子view以参数的形式暴露,供bindView()调用

            view.setTag(cache);

            return view;

        }

    }

    /*

    * 自定义的数据结构,用于存储newView()所生成的view中的元素(各个子view)

    */

    final static class ContactListItemCache {

        public TextView nameView;

        public QuickContactBadge photoView;

        public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);

    }

}