16

我正在开发一个电视指南应用程序,它使用一次ListActivity显示一个频道/一天的电视节目。我正在RelativeLayout为这些ListView项目使用 a,我希望它ListView看起来像这样:

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:minute一部分。start_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 回答