How To Update Listview Whose Data Was Queried From Database Through Simplecursoradapter?
I want to show the items queried from database in the listview with SimpleCursorAdapter. For example, there may be 20,000 items in the database. I want to just load 100 items(_id :
Solution 1:
Use the LIMIT
statement in the SQL query in this way:
SELECT your_column FROM your_table ORDERBY 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:
publicLoader<Cursor> onCreateLoader(int id, Bundle args){
String orderBy = "_id DESC"if(args != null){
orderBy += " LIMIT " + args.getInt("LIMIT_SKIP") + "," + args.getInt("LIMIT_COUNT");
}
returnnewCursorLoader(this/*context*/, CONTENT_URI, PROJECTION, null, null, orderBy);
}
publicvoidonLoadFinished(Loader<Cursor> loader, Cursor data){
listAdapter.swapCursor(data);
}
publicvoidonLoaderReset(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:
Bundleargs=newBundle();
args.putInt("LIMIT_SKIP", limit_skip_value);
args.putInt("LIMIT_COUNT", limit_count_value);
getLoaderManager().restartLoader(0, args, this/*LoaderCallbacks<Cursor>*/);
Post a Comment for "How To Update Listview Whose Data Was Queried From Database Through Simplecursoradapter?"