How to update the list whose data was requested from the database through SimpleCursorAdapter?

I want to show the items requested from a database in a list using SimpleCursorAdapter. For example, a database may have 20,000 items. I just want to load the 100 elements (_id: 1-100) requested instead of loading all the elements, while scrolling at the end of the list, load another 100 elements (_id: 101-200), how to achieve them? Any suggestion is welcome, thanks.

Relative Codes:

protected void onCreate(Bundle savedInstanceState) { mCursor = managedQuery(CONTENT_URI, PROJECTION, null, null, "_id DESC"); mAdapter = new SimpleCursorAdapter(this,R.layout.list_content, mCursor, keys, values); setListAdapter(mAdapter); } 

In my specific view, I want to load more elements into the query base.

 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int lastItem = firstVisibleItem + visibleItemCount - 1; if (mListAdapter != null) { if ((lastItem == mListAdapter.getCount()-1) && (mRefreshState != REFRESHING)) { mFooterView.setVisibility(View.VISIBLE); mRefreshState = REFRESHING; new Handler().postDelayed(new Runnable() { public void run() { //execute the task , i want to load more items by query database RefreshListView(LOADING_STORED_INFO); } }, DEFAULT_DELAY_TIMER); } } } 

In AsyncTask boot data, I perform a query operation.

  protected Integer doInBackground(Integer... params) { Uri uri = ContentUris.withAppendedId(CONTENT_URI, mCursor.getInt(0)-1); cursor = managedQuery(uri, PROJECTION, null, null, "_id DESC"); return (0 == params[0]) ? 1 : 0; } @Override protected void onPostExecute(Integer result) { mAdapter.changeCursor(cursor);//is this OK? mAdapter.notifyDataSetChanged(); /* if (1 == result) { mListView.setSelection(1); } else { mListView.setSelection(mCount-1); }*/ // Call onRefreshComplete when the list has been refreshed. mListView.onRefreshComplete(result); super.onPostExecute(result); } 
+1
android cursor simplecursoradapter
source share
1 answer

Use the LIMIT statement in an SQL query as follows:

 SELECT your_column FROM your_table ORDER BY your_order LIMIT limit_skip, limit_count 

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

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

 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() go null as cursor to new SimpleCursorAdapter() and create a CursorLoader like this:

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

Then in onScroll() , reset the bootloader each time 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>*/); 
+2
source share

All Articles