弄清楚如何对 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;
}
结果如下:
一开始这很好,但给我带来了两个问题:
- 如何用月份动态填充“标题”值?我考虑过使用游标用月份列表填充数组(在使用 SimpleDateFormat 格式化它们之后),然后执行 For Each 循环遍历每个月份,将月份传回游标方法以提取所有条目具有该月的日期时间值。
- 如何将结果连续显示为当天的条目数?理想情况下,我想要这样的东西:
#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”的屏幕截图,这是一款具有自定义分区列表的锻炼应用程序,但基本上是我要复制的内容,至少在功能上是这样。
这是来自的表如下所示:
最佳答案
如何使用 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/