如何使用CursorLoader实现像whatsApp,messenger聊天页面(反向滚动以加载更多)的分页?

时间:2017-05-28 04:54:15

标签: android listview pagination android-contentprovider android-cursorloader

我的OnCreateLoader& onLoadFinished of ChatActivity是:

var science = new HashSet<string> { "GK", "PHY", "CHE", "BIO" };

IQueryable<ScoreInfo> score = db.ScoreInfos
    .OrderBy(m => science.Contains(m.Subject) ? "S" : "A")
    .ThenBy(x => x.Id)
    .Take(5);

此代码适用于整个chatList。但是我需要首次加载ex:20项,并且在顶部的每个反向滚动上加载另外20个项目,就像WhatsApp一样显示在列表中。

是否可以使用如下的rawquery实现这一点?使用下面的方法我得到了预期的数据,但无法返回@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); }类型的数据,

Loader<Cursor>

onScroll to LoadMore机制代码如:

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
}

0 个答案:

没有答案