我正在使用使用一个电视指南应用程序 ListActivity 一次显示电视节目一次 /一天。我正在使用 RelativeLayout 为了 ListView 物品,我想要 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 格式?

有帮助吗?

解决方案

您可以做这样的事情:

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;
    }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top