ResourceCursorTreeAdapter

来源:互联网 发布:sort数组排序 编辑:程序博客网 时间:2024/06/07 14:12

public abstract class
ResourceCursorTreeAdapter
extends CursorTreeAdapter
java.lang.Object
        android.widget.BaseExpandableListAdapter
              android.widget.CursorTreeAdapter
                    android.widget.ResourceCursorTreeAdapter
A fairly simple ExpandableListAdapter that creates views defined in an XML file. 
You can specify the XML file that defines the appearance of the views. 
ResourceCursorTreeAdapter继承于CursorTreeAdapter。
它为cursor和ExpandableListView提供了连接的桥梁。

它让用户提供Group和child的布局文件id,然后它自己就可以为用户创建Group和child的View
ResourceCursorTreeAdapter的子类有
                SimpleCursorTreeAdapter。
他实现了来自于CursorTreeAdapter的两个方法:
newChildView,newGroupView
但使用CursorTreeAdapter还需要实现下面三个方法

Protected Methods
abstract void     bindChildView(View view, Context context, Cursor cursor, boolean isLastChild)
    Bind an existing view to the child data pointed to by cursor
abstract void     bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded)
    Bind an existing view to the group data pointed to by cursor.
abstract Cursor     getChildrenCursor(Cursor groupCursor)
    Gets the Cursor for the children at the given group.
:关于bindChildView,bindGroupView,的机制和CursorAdapter差不多。
具体可参考《CursorAdapter》
实例1
import android.app.ExpandableListActivity;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ResourceCursorTreeAdapter;
import android.widget.TextView;
public class ExpandableListSample3Xml extends ExpandableListActivity {
    private int mGroupIdColumnIndex; 
    private String mPhoneNumberProjection[] = new String[] {
            Phone._ID, Phone.NUMBER
    };
    private ExpandableListAdapter mAdapter;
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Query for people
        Cursor groupCursor = managedQuery(Phone.CONTENT_URI,
                new String[] {Phone._ID, Phone.DISPLAY_NAME}, null, null, null);
        // Cache the ID column index
        mGroupIdColumnIndex = groupCursor.getColumnIndexOrThrow(Phone._ID);
        // Set up our adapter
        mAdapter = new MyExpandableListAdapter(this, groupCursor,
                android.R.layout.simple_expandable_list_item_1,//这个是系统的
                android.R.layout.simple_expandable_list_item_1);//这个是系统的
        setListAdapter(mAdapter);
    }
    public class MyExpandableListAdapter extends ResourceCursorTreeAdapter {
        public MyExpandableListAdapter(Context context, Cursor cursor, int groupLayout, int childLayout) {
            super(context, cursor, groupLayout, childLayout);
            // TODO Auto-generated constructor stub
        }
//绑定child项数据到视图
        @Override
        protected void bindChildView(View view, Context context, Cursor cursor, boolean isExpanded) {
            ((TextView)view).setText(cursor.getString(cursor.getColumnIndex(Phone.NUMBER)));
        }
//绑定group项数据到视图
        @Override
        protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
            ((TextView)view).setText(cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)));
        }
        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            // Given the group, we return a cursor for all the children within that group 
            // Return a cursor that points to this contact's phone numbers
    //查询得到child项的Cursor
            Uri.Builder builder = Phone.CONTENT_URI.buildUpon();
            ContentUris.appendId(builder, groupCursor.getLong(mGroupIdColumnIndex));
            Uri phoneNumbersUri = builder.build();

            // The returned Cursor MUST be managed by us, so we use Activity's helper
            // functionality to manage it for us.
            return managedQuery(phoneNumbersUri, mPhoneNumberProjection, null, null, null);
        }
    }
}

from:http://blog.csdn.net/hudashi/article/details/6889106

0 0