My OnCreateLoader & onLoadFinished of ChatActivity are:
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader loader = new CursorLoader(this,
DataProvider.CONTENT_URI_MESSAGES,
new String[]{
DataProvider.KEY_ID,
DataProvider.KEY_PHONE_NUMBER,
DataProvider.KEY_USER,
DataProvider.KEY_FLAG,
DataProvider.KEY_DATE_TIME,
DataProvider.KEY_CONTENT
},
"phone_number=?", new String[]{phoneNo_other}, null);
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mChatListBuilderAdapter.swapCursor(data);
}
This code is working well for whole chatList. But I need to load ex:20 items at first time and on every reverse scroll at top reached load another 20 items to show on list just like WhatsApp.
Is it possible to implement this using rawquery like below? Using below method I got expected data but can't return Loader<Cursor>
type data,
public Loader<Cursor> loadInitialItems(String phone_number, int limit){ //// limit 100, 200` -- get 200 records beginning with row 101 //
SQLiteDatabase db = this.getWritableDatabase();
String queryToGetInitialData ="SELECT * FROM (SELECT * FROM "+MYDATABASE_TABLE+" WHERE "+KEY_PHONE_NUMBER+" = ? ORDER BY "+KEY_ID+" DESC LIMIT "+limit+") ORDER BY "+KEY_ID+" ASC";
Cursor cursor = db.rawQuery(queryToGetInitialData, new String[] { phone_number });
String chatContent = "";
if (cursor.moveToFirst()) {
do {
chatContent = cursor.getString(cursor.getColumnIndex(DataProvider.KEY_CONTENT));
Log.d(TAG,"chatContent from DB::"+chatContent);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return (Loader<Cursor>) cursor; //Problem is here, this return is inappropriate
}
onScroll to LoadMore mechanism code is like:
messagesList.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0) {
if(!flag_loading) {
flag_loading = true;
loadNextChatItems();
}
}
}
});