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);
}
}
}