Filtering a ListView with a CursorLoader and Custom CursorAdapter

I'm currently doing a project that includes showing a list of locations near my current location.

I just started Android programming recently, so I'm still in the learning process at the coding stage.

I searched everything, trying to figure out how to act. I'm still stuck after reading and testing.

Currently my working code consists of

  • CursorLoader
  • Custom ResourceCursorAdapter that helps populate my ListView entries

Questions

  • What is the β€œright” way to filter records for my ListView? I saw messages in the Filter / Filterable interface, but it doesn't seem to work for my current setup? Filtering inside your custom CursorAdapter adapter?

  • How do I update a ListView after filtering? I call getLoaderManager (). RestartLoader (0, null, this) or adapter.notifyDataSetChanged ()?

Thanks in advance.

+6
source share
1 answer

Use getLoaderManager().restartLoader(LOADER_ID, null, MainActivity.this); to call onCreateLoader .

An example of an Android developer site .

 private String filter; public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_filter : filter = "COLUMN_NAME = value"; getLoaderManager().restartLoader(LOADER_ID, null, MainActivity.this); break; default: break; } return super.onOptionsItemSelected(item); } @Override public android.content.Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader( MainActivity.this, // Parent activity context SomeContentProvider.CONTENT_URI, // Table to query projection, // Projection to return filter, // No selection clause null, // No selection arguments null // Default sort order ); } 
+12
source

Source: https://habr.com/ru/post/954947/


All Articles