1

我正在使用简单的光标适配器来填充列表视图。我的数据来自 sqlyte 数据库,我有两个数据。如何在列表视图中添加部分(标题)。

我搜索了很多,但使用简单的光标适配器找不到合适的部分示例。任何帮助是极大的赞赏。这是我的例子。

开始管理光标(光标);

String[] from = {"Label","_id"};

int[] to = new int[]{R.id.Text1};

SimpleCursorAdapter cursorAdapter =
    new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

listContent.setAdapter(cursorAdapter);

我需要添加部分,例如

第 1 节 橙色 香蕉 苹果 Sextion 2 红色 蓝色 黑色 第 3 节 早餐 午餐

4

1 回答 1

1

这实际上很难做到,因为您需要重新映射光标位置。有一个图书馆可以帮助你做到这一点。以下是可用于使用此库实现字母部分的代码。

@Override 
protected Object getSectionFromCursor(Cursor cursor) {
    int columnIndex = cursor.getColumnIndex("Label");
    String name = cursor.getString(columnIndex);
    return name.toUpperCase().substring(0, 1); 
}

还可以查看他们按部门对员工进行排序的示例应用程序。

https://github.com/twotoasters/SectionCursorAdapter

于 2014-04-10T12:21:57.993 回答