修改SimpleCursorAdapter的数据

时间:2011-01-23 22:17:01

标签: android android-listview simplecursoradapter android-viewbinder

我正在开发一个电视指南应用程序,该应用程序使用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格式?

1 个答案:

答案 0 :(得分: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;
    }
});
相关问题