CursorLoader with ContentProvider not working

497 views Asked by At

I have this ContentProvider:

public class UserContentProvider extends ContentProvider {
    private static final String BASE_AUTHORITY = "com.myapp.providers.";
    private static final String AUTHORITY = BASE_AUTHORITY + "UserContentProvider";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/users");
    public static final String[] PROJECTION = {
        "_id",
        "name"
    };

    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        URI_MATCHER.addURI(AUTHORITY, "users", 1);
    }

    private DatabaseAdapter dbAdapter;

    @Override
    public boolean onCreate() {
        dbAdapter = new DatabaseAdapter(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables("users");

        SQLiteDatabase db = dbAdapter.getReadableDatabase();

        Cursor cursor = builder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);

        return cursor;
    }

    //Other overrides removed for readability
}

I'm using this content provider only for querying the db.

I also have a DialogFragment:

public class UserSearchDialog extends DialogFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private EditText editTextSearch;
    private ListView listViewUsers;
    private SimpleCursorAdapter adapter;

    public UserSearchDialog () {}

    public static UserSearchDialog newInstance() {
        return new UserSearchDialog ();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //dialog creation code removed for readability...

        editTextSearch= (EditText)view.findViewById(R.id.editText_search);
        editTextSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                String text = new String(s.toString());

                if (text== null || text.length() < 2)
                    return;

                getLoaderManager().restartLoader(1, null, UserSearchDialog.this);
            }
        });

        adapter = new SimpleCursorAdapter(getActivity(),
                                          android.R.layout.simple_list_item_2,
                                          null,
                                          new String[] { "_id", "name" },
                                          new int[] { android.R.id.text1, android.R.id.text2 },
                                          0);

        listViewUsers = (ListView)view.findViewById(R.id.listView_users);
        listViewUsers.setAdapter(adapter);

        getLoaderManager().initLoader(1, null, this);

        return alertDialogBuilder.create();
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        if (id != 1)
            return null;

        return new CursorLoader(getActivity(),
                                UserContentProvider.CONTENT_URI,
                                UserContentProvider.PROJECTION_SIMPLE, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if (adapter != null && data != null)
            adapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        if (adapter != null)
            adapter.swapCursor(null);
    }
}

However, the above is not working. Whilst debugging, the content provider' onCreate and query method are never reached.

in the manifest I have:

<provider android:authorities="com.myapp.providers"
    android:exported="false"
    android:name="com.myapp.providers.UserContentProvider">
</provider>

What am I missing?

0

There are 0 answers