android - 动态创建一个包含每个月标题行的 ListView

标签 android listview android-listview

弄清楚如何对 ListView 进行分段让我很头疼。我在这里看到了分段列表适配器的代码:ListView with scrolling/fixed header rows这可能最终是我想要的,但也许有更好的方法。

这是我需要的要求:

  • ListView 的数据需要来自 SQLite 数据库(表格布局见下面的代码)
  • 数据应按月分组(月/年加分)
  • 标题行应包含该月数据库中记录的项目数
  • 必须与 API 8+ 兼容
  • 需要能够单击一个项目以打开包含当天执行的 Action 的对话框(我已经知道如何在创建列表后执行此操作)

在这里查看 Jeff Snarkey 的 seperatedListAdapter:http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/我能够想出以下内容:

datasource = new SmashDataSource(this);
    datasource.open();
    BJJHistory = (ListView) findViewById(R.id.ListHistory);

    // create our list and custom adapter
    adapter = new SeparatedListAdapter(this);
    HistoryBJJ = datasource.getBJJHistory();
    // THE DESIRED COLUMNS TO BE BOUND
    final String[] columns = new String[] { SQLiteHelper.DATE };

    // THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
    final int[] to = new int[] { R.id.list_item_title };
    if (HistoryBJJ != null) {
        adapter.addSection("October", new SimpleCursorAdapter(this, R.layout.list_item,
                HistoryBJJ, columns, to));
    }

    BJJHistory.setAdapter(adapter);

这使用以下游标按降序从 SQLite 数据库中提取数据:

public Cursor getBJJHistory() {
    final String[] columns = { SQLiteHelper.COLUMN_ID, SQLiteHelper.DATE };
    final Cursor History;
    History = database.query(SQLiteHelper.TABLE_BJJ, columns, null, null, null, null,
            SQLiteHelper.DATE + " DESC");
    return History;

}

结果如下:

sample list

一开始这很好,但给我带来了两个问题:

  • 如何用月份动态填充“标题”值?我考虑过使用游标用月份列表填充数组(在使用 SimpleDateFormat 格式化它们之后),然后执行 For Each 循环遍历每个月份,将月份传回游标方法以提取所有条目具有该月的日期时间值。
  • 如何将结果连续显示为当天的条目数?理想情况下,我想要这样的东西: daterow

#2 的答案有点简单,只需在 ListView 行布局中放置两个 TextView ,更复杂的是如何每天对数据库中的所有行进行分组,或者至少对每一天进行计数并使用该计数显示在列表中。

首先,回到此处的示例代码:http://code.google.com/p/android-section-list/ ,而不是提供的示例数组,我想我可以更改以下内容:

SectionListItem[] exampleArray = { // Comment to prevent re-format
new SectionListItem("Test 1 - A", "A"), //
        new SectionListItem("Test 2 - A", "A"), //
        new SectionListItem("Test 3 - A", "A"), //
        new SectionListItem("Test 4 - A", "A"), //
        new SectionListItem("Test 5 - A", "A"), //
        new SectionListItem("Test 6 - B", "B"), //
        new SectionListItem("Test 7 - B", "B"), //
        new SectionListItem("Test 8 - B", "B"), //
        new SectionListItem("Test 9 - Long", "Long section"), //
        new SectionListItem("Test 10 - Long", "Long section"), //
        new SectionListItem("Test 11 - Long", "Long section"), //
        new SectionListItem("Test 12 - Long", "Long section"), //
        new SectionListItem("Test 13 - Long", "Long section"), //
        new SectionListItem("Test 14 - A again", "A"), //
        new SectionListItem("Test 15 - A again", "A"), //
        new SectionListItem("Test 16 - A again", "A"), //
        new SectionListItem("Test 17 - B again", "B"), //
        new SectionListItem("Test 18 - B again", "B"), //
        new SectionListItem("Test 19 - B again", "B"), //
        new SectionListItem("Test 20 - B again", "B"), //
        new SectionListItem("Test 21 - B again", "B"), //
        new SectionListItem("Test 22 - B again", "B"), //
        new SectionListItem("Test 23 - C", "C"), //
        new SectionListItem("Test 24 - C", "C"), //
        new SectionListItem("Test 25 - C", "C"), //
        new SectionListItem("Test 26 - C", "C"), //

并将其替换为游标或不需要按天提取数据的内容,然后将“A”、“B”、“C”替换为月份名称。

这让我很困惑,因为我还在学习,我几乎已经完成了这个应用程序的每个部分,我只是不知道如何将数据分成列表

作为引用,这是“CardioTrainer”的屏幕截图,这是一款具有自定义分区列表的锻炼应用程序,但基本上是我要复制的内容,至少在功能上是这样。

cardio trainer screenshot

这是来自的表如下所示: datatable

最佳答案

如何使用 https://github.com/commonsguy/cwac-merge而不是 http://code.google.com/p/android-section-list/ .

我怀疑您是否有要求在滚动时一个部分应该停留在顶部。

其次使用合并适配器很简单。

addView(您的剖面 View ) addAdapter(您的特定部分适配器)

这应该让你继续:)

希望对您有所帮助。

关于android - 动态创建一个包含每个月标题行的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12719581/

相关文章:

java - Android编程——ListView中的多行EditText

android - 在多选模式下在 ListView 中启用/禁用项目选择

android - eclipse : How to restore default settings in perspectives?

android - 如何执行 findViewById (R.id.>> StringVarHere << )?

Java:获取 "hh:MM:ss a"格式的时间

Android AutoCompleteTextView 作为 ListView header

android - 访问 wifi.getScanResults() 中的结果时发生崩溃

java - getView 未在自定义适配器中调用

Android ExpandableListView 分隔符是不可见的

java - 即使应用在线,也先查询 Firestore 离线数据