2012-10-04 47 views
4

搞清楚如何对列表视图进行分区正在踢我的屁股。我在这里看到了分区列表适配器的代码:ListView with scrolling/fixed header rows,这可能最终是我想要的,但也许有更好的方法。动态创建每个月的标题行列表视图

这里有我需要的要求:

  • 数据的列表视图需要来自SQLite数据库(请参阅下面的代码表布局)
  • 数据应该按月(用于分组积分月/年)
  • 标题行应该包含在数据库中记录该月
  • 项目的数量必须与API兼容8+
  • 需要能够单击一个项目,打开拨号OG包含在这一天进行的移动(我已经知道如何做到这一点,一旦我能得到一个列表中创建)

这里杰夫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的答案有些很简单,只需将两个textviews在ListView行布局,更重要的是复杂的是如何组数据库中的所有行对于每一天,或者至少每天进行一次计数,并使用该计数显示在列表中。

对于第一,要追溯到示例代码在这里: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

这是从外观来像这样的表: ​​

+1

首先,当您需要自定义节标题时,我建议不要使用其他人的适配器。在列表中实施章节并不是很困难。我只需要一件事情。当你检索一个光标到数据库进行显示时,你是否按时间排列所有数据?当一年中有变化时,你会做什么?例如,您将展示2011年6月至12月的部分,那么2012年1月的日期到来时会发生什么? –

回答