AsyncQueryHandler は abstract クラスなので、継承したクラスを作って、onQueryComplete() や onQueryInsertComplete() を Override し、ここで Adapter に Cursor をセットします。
こんな感じで使います。
public class AsyncQueryHandlerSampleActivity extends ListActivity {
private static final String[] PHONE_PROJECTION = new String[] {
Phone._ID,
Phone.TYPE,
Phone.NUMBER
};
private static final int COLUMN_TYPE = 1;
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 CustomAsyncQueryHandler extends AsyncQueryHandler {
public CustomAsyncQueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
super.onQueryComplete(token, cookie, cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
AsyncQueryHandlerSampleActivity.this,
android.R.layout.simple_list_item_2,
cursor,
new String[] { Phone.TYPE, Phone.NUMBER },
new int[] { android.R.id.text1, android.R.id.text2 });
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == COLUMN_TYPE) {
int type = cursor.getInt(COLUMN_TYPE);
String text = (String) Phone.getTypeLabel(getResources(), type, null);
((TextView) view).setText(text);
((TextView) view).setTextColor(COLOR_LIST[type % 5]);
return true;
}
return false;
}
});
setListAdapter(adapter);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomAsyncQueryHandler asyncQueryHandler = new CustomAsyncQueryHandler(getContentResolver());
asyncQueryHandler.startQuery(
0, // token
null, // cookie
Phone.CONTENT_URI,
PHONE_PROJECTION, // projection
null, // selection
null, // selectionArgs
null // orderBy
);
}
}
0 件のコメント:
コメントを投稿