java - How to reverse scroll through SQLITE database table at the top of the table -
i storing messages chat group in table dedicated group.
now, time open group in listview, displays last n messages using code:
public arraylist<post> getrecentposts(int numofmessagesgottenalready,int numofmessagesneeded) { sqlitedatabase db = this.getreadabledatabase(); cursor res = db.rawquery("select * "+room_table_name+ " limit "+numofmessagesneeded + " offset (select count(*) "+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 need load more messages group,i call above method supplies number of messages loaded first argument , number of messages need loaded second argument.
the method works (or think), loading messages bottom of table upwards of course. there hitch.
when gets top of table i.e. offset=0and method called again, offset becomes negative , keeps loading earliest messages @ top of table specified @ https://www.sqlite.org/lang_select.html.
i want when offset evaluates value less zero, no value returned in cursor.
how modify expression achieve please?
okay, solved problem using 2 separate queries:
i wrapped whole process described below in transaction.
i first obtained number of messages on table. checked using java code if there more messages loaded..and based on result, loaded more messages. able detect if had gotten top of table , write necessary code return empty list in event:
here code: can used implement whatsapp's load earlier messages feature...where loading done sqlite table.
/** * useful loading chatroom has messages loaded bottom of * table. allows 'load more' messages(just number needed) without loading * whole chatroom. * @param numofmessagesgottenalready number of messages room has already. * @param numofmessagesneeded number of messages load more. * @return arraylist of messages loaded in pagination style..from bottom time. */ public arraylist<post> getrecentposts(int numofmessagesgottenalready,int numofmessagesneeded) { sqlitedatabase db = this.getreadabledatabase(); int add = numofmessagesgottenalready + numofmessagesneeded; boolean success = true; try { db.begintransaction(); cursor res = db.rawquery("select count(*) "+room_table_name , null); res.movetofirst(); int count = res.getint(0); if (!res.isclosed()) { res.close(); } log.e("table row count", count + ""); log.e("add", add + ""); if(count >= numofmessagesgottenalready) { cursor set = db.rawquery("select * " + room_table_name + " order " + rooms_column_id + " asc limit " + numofmessagesneeded + " offset ( " + (count - add) + " )" , null); set.movetofirst(); arraylist<post>posts = new arraylist<>(); try { while (!set.isafterlast()) { posts.add(post.parsejsontopost( set.getstring(set.getcolumnindex(rooms_column_json)))); set.movetonext(); } } catch (exception e){return posts;} if (!set.isclosed()) { set.close(); } return posts; } db.settransactionsuccessful(); success = true; } catch (sqliteexception e){ e.printstacktrace(); success = false; } { db.endtransaction(); } return new arraylist<post>(); } thanks!
Comments
Post a Comment