在 Android 中为 SimpleCursorAdapter 添加日期分隔符

发布于 2024-11-17 10:45:06 字数 1009 浏览 4 评论 0原文

我在我的视图上设置了一个 SimpleCursorAdapter ,当我从数据库中获取数据时它工作正常。该查询使用 ORDER BY date DESC 子句运行。所有行都被提取并在我的 ListView 中很好地显示,并且我正在使用adapter.setViewBinder() 向项目添加一些逻辑(添加缩略图)。

代码是这样的:

// Setup the adapter
adapter = new SimpleCursorAdapter(this, R.layout.rowitem, cursor, FROM, TO);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
  @Override
  public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
    // Only taylor the ImageView in the adapter-list, skip everything else.
    if (view.getId() != R.id.imageItem)
      return false;

    // here goes the logic, but it's cut away due to its size

    }

    return true;
  }
});

现在我想添加更多逻辑:如果日期在两个项目之间更改,我想添加一个包含日期的小分隔栏,如下所示:

=== DATE1 ===============
Item 1
Item 2
=== DATE2 ===============
Item 3
=== DATE3 ===============
Item 4
Item 5
Item 6
=== DATE4 ===============
Item 7

等等。但是,我不确定如何在适配器中插入新的视图元素,并且我似乎无法在developer.android.com上找到答案。

我怎样才能做到这一点?

I have setup a SimpleCursorAdapter on my view and it works fine when I'm fetching the data from my database. The query is run using a ORDER BY date DESC-clause. All rows are fetched and displayed nicely in my ListView, and I'm using adapter.setViewBinder() to add some logic to the items (adding a thumbnail).

The code is like this:

// Setup the adapter
adapter = new SimpleCursorAdapter(this, R.layout.rowitem, cursor, FROM, TO);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
  @Override
  public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
    // Only taylor the ImageView in the adapter-list, skip everything else.
    if (view.getId() != R.id.imageItem)
      return false;

    // here goes the logic, but it's cut away due to its size

    }

    return true;
  }
});

Now I want to add some more logic: I want to add a small separator bar containing the date if the date is changed between two items, like this:

=== DATE1 ===============
Item 1
Item 2
=== DATE2 ===============
Item 3
=== DATE3 ===============
Item 4
Item 5
Item 6
=== DATE4 ===============
Item 7

and so on. However, I'm not sure how to insert a new View-element inside of an adapter and I cannot seem to find an answer on developer.android.com.

How can I accomplish this?

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

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

发布评论

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

评论(2

給妳壹絲溫柔 2024-11-24 10:45:06

您还可以查看我开发的更强大的解决方案:http://code。 google.com/p/android-section-list/。它的功能甚至比您需要的多一点(尽管更好) - 它的工作方式是添加一个特殊的适配器来包装原始适配器并在需要时注入节标题(在您的情况下是日期标题)。标题保持在列表顶部(类似于 iphone 部分列表) - 这样您就可以始终看到您所在的部分......

您可以使用部分列表作为库 - 您只需要更改您的适配器以返回SectionListItem(并将日期放入部分),然后将其添加到SectionListView - 这基本上可以完成这项工作......

You can also look at a bit more robust solution which I developed: http://code.google.com/p/android-section-list/. It does even a bit more than you need (albeit nicer) - it works in the way that it adds a special adapter wrapping the original one and injecting section headers (in your case date headers) when needed. The headers remain sticky at top of the list (similarly as in iphone section lists) - so that you can always see which is the section you are in.....

You can use the section list as library - You would just have to change your adapter to return SectionListItem (and putting date into section) and then add it to SectionListView - that would do the job basically....

不如归去 2024-11-24 10:45:06

-- 编辑 --

好的,这是解决此问题的一个简单解决方案:

使用日期作为SectionHeader的自定义ListView(使用自定义SimpleCursorAdapter)

--- 旧帖子 ---
我无法对您的第一篇文章发表评论,但我想请问您是否可以更详细地解释一下您如何设法获得带有日期标题的列表。
您必须迭代所有的光标行,或者框架是否提供了更好的方法?

我有很多项目,如果我使用 while(listData.moveToNext()){ 进行迭代,则需要很长时间。 (我还必须从unix时间戳到日历、日期、月份整数进行计算)。

-- Edit --

Okay here is an easy solution for this problem:

Custom ListView with Date as SectionHeader (Used custom SimpleCursorAdapter)

--- old post ---
I couldn't comment on your first Post but I wanted to kindly ask you if you could explain in a little more detail how you managed to get this list with date headers.
You would have to iterate over all the curser-rows or is there a better way provided by the framework?

I have quite a few items and if I iterate with while(listData.moveToNext()){ it takes to long. (I also have to calculate from a unixtime stamp to a Calendar, to a Date, to a month-Int).

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