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.

1 Answer 1

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

    }
1
  • 2
    A bit sparse on words, but a useful concept. Upvote.
    – Anderson
    Jul 25, 2014 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.