16

私はListActivity、一度に1つのチャンネル/1日のテレビ番組を表示するTVガイドアプリに取り組んでいます。アイテムにを使用しRelativeLayoutていますが、次のように表示したいと思います。ListViewListView

07:00 The Breakfast Show
      Latest news and topical reports
08:00 Tom and Jerry
      More cat and mouse capers

ListView次のコードを使用してアイテムのデータを取得します。

Cursor cursor = db.rawQuery(SELECT blah,blah,blah);
String[] columnNames = new String[]{"start_time","title", "subtitle"};
int[] resIds = new int[]{R.id.start_time_short, R.id.title, R.id.subtitle};
adapter = new SimpleCursorAdapter(this, R.layout.guide_list_item, cursor, columnNames, resIds);

私の問題は、start_timeフィールドがdatetime次の形式であるということです。

2011-01-23 07:00:00

だから私が得るものはこれです:

2011-01-23 07:00:00 The Breakfast Show
                    Latest news and topical reports
2011-01-23 08:00:00 Tom and Jerry
                    More cat and mouse capers

私がやりたいのは、SimpleDateFormat( )を使用して上記をフォーマットすることです。これにより、フィールドの一部"HH:mm"のみが取得されます。hour:minutestart_time

私はSimpleCursor.ViewBinderそれが私が望むものかもしれないことを示唆するインターフェースを見つけましたが、それを使用する方法を理解することができません。私が正しければViewBinder、それを使用する方法についてのサンプルコードへのいくつかのポインタをいただければ幸いです。start_timeそれ以外の場合、単にHH:mmフォーマットを表示するようにフィールドを変更するには、他にどのようにすればよいですか?

4

1 に答える 1

28

次のようなことができます。

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
    @Override
    public boolean setViewValue(View view, Cursor cursor, int column) {
        if( column == 0 ){ // let's suppose that the column 0 is the date
            TextView tv = (TextView) view;
            String dateStr = cursor.getString(cursor.getColumnIndex("name_of_the_date_column"));
            // here you use SimpleDateFormat to bla blah blah
            tv.setText(theFormatedDate);
            return true;
        }
        return false;
    }
});
于 2011-01-23T22:25:52.147 に答える