SimpleCursorAdapter.ViewBinder の setViewValue() メソッドでは第2引数としてその行の Cursor と、第3引数として列の columnIndex が渡されるので、これを使ってそのカラムに表示したいデータを取得して、色を変えたり文字の大きさを変えたり表示するデータを別のテーブルからとってきたりして、対象の View にセットします。
- public class SimpleCursorAdapterSampleActivity 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"),
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Cursor c = getContentResolver().query(
- Phone.CONTENT_URI,
- PHONE_PROJECTION, // projection
- null, // selection
- null, // selectionArgs
- null // orderBy
- );
- startManagingCursor(c);
- SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
- android.R.layout.simple_list_item_2,
- c,
- 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);
- }
- }
0 件のコメント:
コメントを投稿