I am storing messages from a chat group in a table dedicated to the group.
Now, any time I open this group in a listview, it displays the last N messages using this code:
public ArrayList<Post> getRecentPosts(int numOfMessagesGottenAlready,int numOfMessagesNeeded) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from "+ROOM_TABLE_NAME+ " LIMIT "+numOfMessagesNeeded +
" OFFSET (SELECT COUNT(*) FROM "+ROOM_TABLE_NAME+")-"+
(numOfMessagesGottenAlready + numOfMessagesNeeded)+";", null);
res.moveToFirst();
ArrayList<Post>posts = new ArrayList<>();
while (!res.isAfterLast()) {
posts.add(Post.parseJsonToPost(
res.getString(res.getColumnIndex(ROOMS_COLUMN_JSON))) );
res.moveToNext();
}
if (!res.isClosed()) {
res.close();
}
return posts;
}
Any time I need to load more messages into the group,I call the above method which supplies the number of messages previously loaded as its first argument and the number of messages that need to be loaded as its second argument.
The method works well (or so I think), loading the messages from the bottom of the table upwards of course. But there is an hitch.
When it gets to the top of the table i.e. OFFSET=0and then the method is called again, the OFFSET becomes negative and so it just keeps loading the earliest messages at the top of the table as specified at http://ift.tt/1MjSvfC.
I want it to be that when the OFFSET evaluates to a value less than zero, then no value will be returned in the Cursor.
How do I modify the expression to achieve this please?
Aucun commentaire:
Enregistrer un commentaire