ListView filtering with CursorLoader and Custom CursorAdapter
Asked Answered
N

1

6

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.

Natka answered 30/9, 2013 at 13:41 Comment(0)
B
14

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

    }
Battleax answered 7/11, 2013 at 20:38 Comment(1)
A bit sparse on words, but a useful concept. Upvote.Ledford

© 2022 - 2024 — McMap. All rights reserved.