How to update listview whose data was queried from database through SimpleCursorAdapter?

时光怂恿深爱的人放手 提交于 2019-12-01 01:39:29

Use the LIMIT statement in the SQL query in this way:

SELECT your_column FROM your_table ORDER BY your_order LIMIT limit_skip, limit_count

Then you can use a OnScrollListener to retrieve the index of the first visible cell and the number of visible cells so you can increment limit_skip and limit_count coherently.

Instead of the generic AsyncTask use a CursorLoader and implement LoaderManager.LoaderCallbacks<Cursor> as follow:

public Loader<Cursor> onCreateLoader(int id, Bundle args){
    String orderBy = "_id DESC"
    if(args != null){
        orderBy += " LIMIT " + args.getInt("LIMIT_SKIP") + "," + args.getInt("LIMIT_COUNT");
    }

    return new CursorLoader(this /*context*/, CONTENT_URI, PROJECTION, null, null, orderBy);
}

public void onLoadFinished(Loader<Cursor> loader, Cursor data){
    listAdapter.swapCursor(data);
}

public void onLoaderReset(Loader<Cursor> loader){
    listAdapter.swapCursor(null);
}

Then, in onCreate(), pass null as cursor to new SimpleCursorAdapter() and create the CursorLoader in this way:

getLoaderManager().initLoader(0, null, this /*LoaderCallbacks<Cursor>*/);

Then, in onScroll(), reset everytime the loader in this way:

Bundle args = new Bundle();
args.putInt("LIMIT_SKIP", limit_skip_value);
args.putInt("LIMIT_COUNT", limit_count_value);
getLoaderManager().restartLoader(0, args, this /*LoaderCallbacks<Cursor>*/);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!