Contentprovider不起作用

时间:2015-10-11 17:56:47

标签: android android-contentprovider

我想让我的自定义ContentProvider工作。我看了几个例子,但找不到我的bug。有人可以看看我的代码并说出我做错了。

的ContentProvider:

package be.drizzlyday.ghosttje.countrylock.app.providers;

public class CountryProvider extends ContentProvider {

    private static DatabaseHelper database;
    private static final String PROVIDER = "be.drizzlyday.ghosttje.countrylock.providers";
    private static final String BASE_PATH = "countryprovider";

    public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER + "/" + BASE_PATH);

    public static final String COUNTRIES = "/countries";
    public static final String COUNTRY_ID = "/country";

    private static final int KEY_COUNTRIES = 10;
    private static final int KEY_COUNTRY_ID = 20;

    private static final String TAG = "CountryProvider";

    private static final UriMatcher URIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        URIMatcher.addURI(PROVIDER, BASE_PATH + COUNTRIES, KEY_COUNTRIES);
        URIMatcher.addURI(PROVIDER, BASE_PATH + COUNTRIES + "/#", KEY_COUNTRY_ID);
    }

    @Override
    public boolean onCreate() {
        database = new DatabaseHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

        int uriType = URIMatcher.match(uri);
        queryBuilder.setTables(DBContract.Countries.TABLE_NAME);

        switch (uriType) {
            case KEY_COUNTRIES:
                Log.d(TAG, "Countries");
                break;
            case KEY_COUNTRY_ID:
                queryBuilder.appendWhere(DBContract.Countries._ID + "=" + uri.getLastPathSegment());
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }

        SQLiteDatabase db = database.getReadableDatabase();
        SQLiteCursor cursor = (SQLiteCursor) queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);

        cursor.setNotificationUri(getContext().getContentResolver(), uri);

        Log.d("Provider", "Cursor: " + cursor.toString()
                + " - Columncount: " + cursor.getColumnCount()
                + " - Rowcount: " + cursor.getCount());

        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}

调用contentprovider:

Uri uri = Uri.parse(CountryProvider.CONTENT_URI + CountryProvider.COUNTRIES);

Cursor cursor = getContentResolver().query(uri, from, null, null, null);

并添加到清单文件中:

<provider
                android:name=".providers.CountryProvider"
                android:authorities="be.drizzlyday.ghosttje.countrylock.app.providers"
                android:exported="false" >
        </provider>

0 个答案:

没有答案