修改SimpleCursorAdapter的数据

发布于 2024-10-14 07:59:05 字数 1336 浏览 5 评论 0原文

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

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

我获取 ListView 的数据>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:min 部分start_time 字段。

我找到了 SimpleCursor.ViewBinder 接口,这表明它可能是我想要的,但我不知道如何使用它。如果我对 ViewBinder 的看法是正确的,我会很感激一些有关如何使用它的示例代码的指示。否则,我还能如何实现更改 start_time 字段以仅显示 HH:mm 格式?

I'm working on a TV Guide app which uses a ListActivity showing the TV shows for one channel / one day at a time. I'm using a RelativeLayout for the ListView items and I want the ListView to look something like this:

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

I get the data for the ListView items using the following code:

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);

My problem is that the start_time field is a datetime with the following format:

2011-01-23 07:00:00

so what I get is this:

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

What I'd like to do is format the above using SimpleDateFormat("HH:mm") so I only get the hour:minute part of the start_time field.

I've found the SimpleCursor.ViewBinder interface which suggests it may be what I want but I can't figure out how to use it. If I'm right about ViewBinder, I'd appreciate some pointers to sample code on how to use it. Otherwise, how else can I achieve changing the start_time field to simply show HH:mm format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆で 2024-10-21 07:59:05

你可以这样做:

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;
    }
});

You can do something like this:

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;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文