Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView filtering with CursorLoader and Custom CursorAdapter

I am currently doing a project that involves showing a list of locations nearby based on my current location.

I just started Android Programming not long ago, so I am still at my learning while coding phase.

I searched all over trying to get some clues on how to proceed. I am still stuck after reading and trying out.

My working code currently consists of

  • CursorLoader
  • A custom ResourceCursorAdapter that help populates my entries on the ListView

Questions

  1. What is the "correct" way to filter entries for my ListView? I saw posts on Filter/Filterable interface, but it doesn't seems to work for my current setup? Do I perform filtering inside my Custom CursorAdapter?

  2. How should I refresh my ListView after I perform filtering? Do I call getLoaderManager().restartLoader(0, null, this) or adapter.notifyDataSetChanged() ?

Thanks in advance.

like image 632
user2831334 Avatar asked Sep 30 '13 13:09

user2831334


1 Answers

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

Android developer site example.

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
                );

    }
like image 87
kubo Avatar answered Sep 28 '22 06:09

kubo