我需要在列表视图中显示SQLite结果.当然,我需要分页结果.
第一种选择是使用LIMIT子句.例如:
SELECT * FROM Table LIMIT 100, 5000
Run Code Online (Sandbox Code Playgroud)
它返回记录5001到5100.问题是内部SQLite"读取"前5000条记录并且效率不高.
当有大量记录时,分页的最佳方法是什么?
我正在使用以下代码获取通知列表(总行数为21):
List<Notification> list = new ArrayList<Notification>();
Cursor c = _db.query(TABLE_NAME, COL_ALL, null, null, null, null, order, get_limitStr(offset));
if(c != null && c.moveToFirst())
{
while(!c.isAfterLast())
{
Notification model = cursorToModel(c);
if(model != null)
{
list.add(model);
}
c.moveToNext();
}
c.close();
}
Run Code Online (Sandbox Code Playgroud)
并且生成的offset = 0的查询是
SELECT Id, Token, Title, Read, Message, Image, CreateDate, CreateDateFA FROM Notifications ORDER BY CreateDate DESC LIMIT 20,0
Run Code Online (Sandbox Code Playgroud)
并且它按预期工作并返回20行,当我将偏移量增加1(偏移= 1)时,它只返回1行,这是正确的,但问题是当偏移量大于1时,查询将是
SELECT Id, Token, Title, Read, Message, Image, CreateDate, CreateDateFA FROM Notifications ORDER BY CreateDate DESC LIMIT 20,2 …Run Code Online (Sandbox Code Playgroud)