修改SimpleCursorAdapter的数据

Squ*_*onk 16 android android-listview simplecursoradapter android-viewbinder

我正在制作一个电视指南应用程序,它使用一次ListActivity显示一个频道/一天的电视节目.我使用RelativeLayoutListView项目,我想ListView看看是这样的:

07:00 The Breakfast Show
      Latest news and topical reports
08:00 Tom and Jerry
      More cat and mouse capers
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

我的问题是该start_time字段datetime具有以下格式:

2011-01-23 07:00:00
Run Code Online (Sandbox Code Playgroud)

所以我得到的是:

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
Run Code Online (Sandbox Code Playgroud)

我想做的是使用SimpleDateFormat("HH:mm")格式化上面所以我只得到hour:minutestart_time字段的一部分.

我发现SimpleCursor.ViewBinder界面表明它可能是我想要的,但我无法弄清楚如何使用它.如果我是对的ViewBinder,我会感谢一些关于如何使用它的示例代码的指示.否则,我怎样才能实现更改start_time字段以显示HH:mm格式?

Cri*_*ian 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;
    }
});
Run Code Online (Sandbox Code Playgroud)