同じように、2段のリストを作る ExpandableListView 用の Adapter として、Object の配列やリストをひもづける SimpleExpandableListAdapter というのがあります。 実は、Cursor データを ExpandableListView にひもづけるための実装クラスの Adapter はありません。変わりに抽象クラスの SimpleCursorTreeAdapter というものがあります。
# SimpleExpandableListAdapter の使い方は API Demos の ExpandableList3.java がわかりやすいです。
このクラスを継承した実装クラスを作って利用します。
public class SimpleCursorTreeAdapterSampleActivity extends ExpandableListActivity {
private static final String[] CONTACTS_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME
};
private static final int GROUP_ID_COLUMN_INDEX = 0;
private static final String[] PHONE_PROJECTION = new String[] {
Phone._ID,
Phone.TYPE,
Phone.NUMBER
};
public class ExpandableListAdapter extends SimpleCursorTreeAdapter {
public ExpandableListAdapter(Context context, Cursor c,
int groupLayout, String[] groupFrom, int[] groupTo,
int childLayout, String[] childrenFrom,int[] childrenTo) {
super(context, c, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom, childrenTo);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
Uri.Builder builder = Contacts.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, groupCursor.getLong(GROUP_ID_COLUMN_INDEX));
builder.appendEncodedPath(Contacts.Data.CONTENT_DIRECTORY);
Uri phoneNumbersUri = builder.build();
Cursor c = getContentResolver().query(phoneNumbersUri, PHONE_PROJECTION, Phone.MIMETYPE + "=?", new String[] { Phone.CONTENT_ITEM_TYPE}, null);
startManagingCursor(c);
return c;
}
}
private SimpleCursorTreeAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c = getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_PROJECTION,
Contacts.HAS_PHONE_NUMBER + "=1", null, null);
mAdapter = new ExpandableListAdapter(
this, c,
android.R.layout.simple_expandable_list_item_1,
new String[] { Contacts.DISPLAY_NAME },
new int[] { android.R.id.text1 },
android.R.layout.simple_expandable_list_item_1,
new String[] { Phone.NUMBER },
new int[] { android.R.id.text1 });
startManagingCursor(c);
setListAdapter(mAdapter);
}
}
SimpleCursorTreeAdapter にも SimpleCursorAdapter 同様 ViewBinder を設定して表示方法を変更することができます。
public class SimpleCursorTreeAdapterSampleActivity extends ExpandableListActivity {
private static final String[] CONTACTS_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME
};
private static final int GROUP_ID_COLUMN_INDEX = 0;
private static final String[] PHONE_PROJECTION = new String[] {
Phone._ID,
Phone.TYPE,
Phone.NUMBER
};
private static final int[] COLOR_LIST = new int[] {
Color.parseColor("#002A42"),
Color.parseColor("#3DC3EA"),
Color.parseColor("#99417B"),
Color.parseColor("#F2AE30"),
Color.parseColor("#F2D338"),
};
public class ExpandableListAdapter extends SimpleCursorTreeAdapter {
public ExpandableListAdapter(Context context, Cursor c,
int groupLayout, String[] groupFrom, int[] groupTo,
int childLayout, String[] childrenFrom,int[] childrenTo) {
super(context, c, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom, childrenTo);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
Uri.Builder builder = Contacts.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, groupCursor.getLong(GROUP_ID_COLUMN_INDEX));
builder.appendEncodedPath(Contacts.Data.CONTENT_DIRECTORY);
Uri phoneNumbersUri = builder.build();
Cursor c = getContentResolver().query(phoneNumbersUri, PHONE_PROJECTION, Phone.MIMETYPE + "=?", new String[] { Phone.CONTENT_ITEM_TYPE}, null);
startManagingCursor(c);
return c;
}
}
private SimpleCursorTreeAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c = getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_PROJECTION,
Contacts.HAS_PHONE_NUMBER + "=1", null, null);
mAdapter = new ExpandableListAdapter(
this, c,
android.R.layout.simple_expandable_list_item_1,
new String[] { Contacts.DISPLAY_NAME },
new int[] { android.R.id.text1 },
android.R.layout.simple_expandable_list_item_2,
new String[] { Phone.TYPE, Phone.NUMBER },
new int[] { android.R.id.text1, android.R.id.text2 });
mAdapter.setViewBinder(new SimpleCursorTreeAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (cursor.getColumnName(columnIndex).equals(Phone.TYPE)) {
int type = cursor.getInt(columnIndex);
String text = (String) Phone.getTypeLabel(getResources(), type, null);
((TextView) view).setText(text);
((TextView) view).setTextColor(COLOR_LIST[type % 5]);
return true;
}
return false;
}
});
startManagingCursor(c);
setListAdapter(mAdapter);
}
}
Y.A.M の 雑記帳: Android AsyncQueryHandler を使う - でも触れたように、UI スレッドで Cursor を取得するためのクエリを走らせると UI スレッドをブロックしていまいます。それを避けるために SimpleCursorTreeAdapter に対しても AsyncQueryHandler を使うようにすることができます。
public class AsyncQuerySimpleCursorTreeAdapterSampleActivity extends ExpandableListActivity {
private static final String[] CONTACTS_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME
};
private static final int GROUP_ID_COLUMN_INDEX = 0;
private static final String[] PHONE_PROJECTION = new String[] {
Phone._ID,
Phone.TYPE,
Phone.NUMBER
};
private static final int[] COLOR_LIST = new int[] {
Color.parseColor("#002A42"),
Color.parseColor("#3DC3EA"),
Color.parseColor("#99417B"),
Color.parseColor("#F2AE30"),
Color.parseColor("#F2D338"),
};
private static final int TOKEN_GROUP = 0;
private static final int TOKEN_CHILD = 1;
private static final class QueryHandler extends AsyncQueryHandler {
private CursorTreeAdapter mAdapter;
public QueryHandler(Context context, CursorTreeAdapter adapter) {
super(context.getContentResolver());
this.mAdapter = adapter;
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
switch (token) {
case TOKEN_GROUP:
mAdapter.setGroupCursor(cursor);
break;
case TOKEN_CHILD:
int groupPosition = (Integer) cookie;
mAdapter.setChildrenCursor(groupPosition, cursor);
break;
}
}
}
public class ExpandableListAdapter extends SimpleCursorTreeAdapter {
public ExpandableListAdapter(Context context,
int groupLayout, int childLayout,
String[] groupFrom, int[] groupTo,
String[] childrenFrom,int[] childrenTo) {
super(context, null, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom, childrenTo);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
Uri.Builder builder = Contacts.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, groupCursor.getLong(GROUP_ID_COLUMN_INDEX));
builder.appendEncodedPath(Contacts.Data.CONTENT_DIRECTORY);
Uri phoneNumbersUri = builder.build();
mQueryHandler.startQuery(TOKEN_CHILD, groupCursor.getPosition(), phoneNumbersUri,
PHONE_PROJECTION, Phone.MIMETYPE + "=?",
new String[] { Phone.CONTENT_ITEM_TYPE }, null);
return null;
}
}
private QueryHandler mQueryHandler;
private SimpleCursorTreeAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new ExpandableListAdapter(
this,
android.R.layout.simple_expandable_list_item_1,
android.R.layout.simple_expandable_list_item_2,
new String[] { Contacts.DISPLAY_NAME },
new int[] { android.R.id.text1 },
new String[] { Phone.TYPE, Phone.NUMBER },
new int[] { android.R.id.text1, android.R.id.text2 });
mAdapter.setViewBinder(new SimpleCursorTreeAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (cursor.getColumnName(columnIndex).equals(Phone.TYPE)) {
int type = cursor.getInt(columnIndex);
String text = (String) Phone.getTypeLabel(getResources(), type, null);
((TextView) view).setText(text);
((TextView) view).setTextColor(COLOR_LIST[type % 5]);
return true;
}
return false;
}
});
setListAdapter(mAdapter);
mQueryHandler = new QueryHandler(this, mAdapter);
mQueryHandler.startQuery(TOKEN_GROUP, null, Contacts.CONTENT_URI,
CONTACTS_PROJECTION, Contacts.HAS_PHONE_NUMBER + "=1", null, null);
}
@Override
protected void onDestroy() {
super.onDestroy();
mAdapter.changeCursor(null);
mAdapter = null;
}
}
勉強なりました。有難うございます。
返信削除