(メモ)SimpleCursorAdapterでパラメータを途中でいじってからビューに渡したい時

SimpleCursorAdapter.ViewBinderを継承したクラスを作り、それをSimpleCursorAdapterを継承したクラスのコンストラクタでsetViewBinder(new MyViewBinder());のように与える。
SimpleCursorAdapter.ViewBinderは
public boolean setViewValue(View view, Cursor cursor, int column_index)
を実装する必要があり、列名COL_KEY_1のところをいじりたい時は、cursor.getColumnIndex(COL_KEY_1) == column_indexが成立した時COL_KEY_1のターン!なので、バインド時にCOL_KEY_1と対応させたviewにキャストしていじってsetText()とかすれば良い。


(サンプルコード)今作り掛けのコードのスレ覧の断片
2chではスレID=立った時間のtime_tなのでDBにはスレIDで保存してるけど、表示は時刻表示したいみたいな話

ListAdapter adapter = new ThreadListCursorAdapter(
this,
R.layout.threads_list_row,
cursor,
new String[] {
BoardsDbAdapter.KEY_THREAD_NAME,
BoardsDbAdapter.KEY_THREAD_ID
},
new int[] {
R.id.thread_list_name,
R.id.thread_list_timestamp
}
);
setListAdapter(adapter);

(中略)

static class ThreadListCursorAdapter extends SimpleCursorAdapter {
static class ThreadListViewBinder implements SimpleCursorAdapter.ViewBinder {
private SimpleDateFormat format;
public ThreadListViewBinder() {
format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
}
@Override
public boolean setViewValue(View view, Cursor cursor, int column_index) {
int thread_id_index = cursor.getColumnIndex(BoardsDbAdapter.KEY_THREAD_ID);
if (column_index == thread_id_index) {
int thread_id = cursor.getInt(thread_id_index);
Date date = new Date((long) thread_id * 1000);
TextView text_view = (TextView) view;
text_view.setText(format.format(date));
return true;
}
return false;
}
}
public ThreadListCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
setViewBinder(new ThreadListViewBinder());
}
}

“(メモ)SimpleCursorAdapterでパラメータを途中でいじってからビューに渡したい時” への2件の返信

  1. はじめまして、いつも参考にさせて頂いています。
    BoardsDbAdapter はどこで定義しているものでしょうか。

  2. へっ
    どこで定義もなにも、単にテーブルのカラム名をstatic final Stringで定義してるだけですよ

h_narazaki へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です


The reCAPTCHA verification period has expired. Please reload the page.