CursorTreeAdapter

来源:互联网 发布:org.apache.hadoop.io 编辑:程序博客网 时间:2024/06/05 14:06
Class Overview
An adapter that exposes data from a series of Cursors to an ExpandableListView widget. 
The top-level Cursor (that is given in the constructor) exposes the groups,
 while subsequent Cursors returned from getChildrenCursor(Cursor) expose children within a particular group. 
 The Cursors must include a column named "_id" or this class will not work.

CursorTreeAdapter继承于BaseExpandableListAdapter,它是个虚类。
它为cursor和ExpandableListView提供了连接的桥梁。
CursorTreeAdapter的子类有
        CursorTreeAdapter
            ResourceCursorTreeAdapter
                SimpleCursorTreeAdapter

使用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.
abstract View     newChildView(Context context, Cursor cursor, boolean isLastChild, ViewGroup parent)
    Makes a new child view to hold the data pointed to by cursor.
abstract View     newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent)
Makes a new group view to hold the group data pointed to by cursor.
:关于bindChildView,bindGroupView,newChildView,newGroupView其实的机制和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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.CursorTreeAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableListSample1Xml extends ExpandableListActivity {
    private final String TAG = "Expandable1"; 
    ExpandableListView mExpandableList;
    ExpandableListAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the expandable list view object
        mExpandableList = getExpandableListView();
        // Set our expandable list adapter
        String[] projection = new String[] {
                Phone._ID,
                Phone.DISPLAY_NAME,
                Phone.NUMBER 
                };
        Cursor contactData = managedQuery(
                Phone.CONTENT_URI,
                projection, 
                null, 
                null, 
                null);
        //注意这里也只设置了一级项的数据
        mAdapter = new CursorTreeAdapterExample(contactData, this);
        setListAdapter(mAdapter);
        
        Toast.makeText(this, "Total contacts: " + contactData.getCount(), Toast.LENGTH_LONG);
        Log.d("EXPANDABLE", "Total contacts: " + contactData.getCount());
    }
    /**
     * Adapter implementation
     * @author wk.kho
     *
     */
    public class CursorTreeAdapterExample extends CursorTreeAdapter {
        private int mGroupIdColumnIndex;
        private LayoutInflater mInflater;
        //注意这里的游标是一级项的
        public CursorTreeAdapterExample(Cursor cursor, Context context) {
            super(cursor, context);
            
            mGroupIdColumnIndex = cursor.getColumnIndexOrThrow(Phone._ID);
            mInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        }
        //注意这里的游标是二级项的
        @Override
        protected void bindChildView(View view, Context context, Cursor cursor, boolean isExpanded) {
            // Bind the related data with this child view
            ((TextView)view).setText(cursor.getString(cursor.getColumnIndex(Phone.NUMBER)));
        }
        @Override
        protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
            // Bind the related data with this group view
            ((TextView)view).setText(cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)));
        }
        //注意这里通过一次数据库查询才得到了二级项的数据
        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            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, new String[] {Phone._ID, Phone.NUMBER}, null, null, null);
        }
        @Override
        protected View newChildView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {
            Log.d(TAG, "newChildView");
            
            TextView view = (TextView) mInflater.inflate(android.R.layout.simple_expandable_list_item_1, parent, false);
            
            view.setText("  (" + cursor.getPosition() + ") " + cursor.getString(cursor.getColumnIndex(Phone.NUMBER)));
            
            return view;
        }
        @Override
        protected View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {
            Log.d(TAG, "newGroupView");
            TextView view = (TextView) mInflater.inflate(android.R.layout.simple_expandable_list_item_1, parent, false);
            view.setText("  (" + cursor.getPosition() + ") " + cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)));
            
            return view;
        }
    }
}
原创粉丝点击