查询返回空游标

时间:2019-04-18 12:11:34

标签: java android sqlite

因此,我试图在数据库中显示内容,但显然游标等于null。我的查询有任何问题吗?

我的数据库有三列(ID,名称,日期)。名称和日期应显示在活动上。 我为您提供了ContentProvider和合同的代码。如果您发现任何错误,请告诉我。

ContentProvider

public class EventProvider extends ContentProvider {


private static final int EVENTS = 100;
private static final int EVENT_ID = 101;

private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

static {
    sUriMatcher.addURI(EventContract.CONTENT_AUTHORITY, EventContract.PATH_EVENTS, EVENTS);
    sUriMatcher.addURI(EventContract.CONTENT_AUTHORITY, EventContract.PATH_EVENTS + "/#", EVENT_ID);
}

public static final String LOG_TAG = EventProvider.class.getSimpleName();

private EventDbHelper mDbHelper = new EventDbHelper(getContext());

@Override
public boolean onCreate() {
    mDbHelper = new EventDbHelper(getContext());
    return true;
}

@Override
public Cursor query(Uri uri, String[] projection,  String selection, String[] selectionArgs, String sortOrder) {
    SQLiteDatabase database = mDbHelper.getReadableDatabase();

    // This cursor will hold the result of the query
    Cursor cursor;

    // Figure out if the URI matcher can match the URI to a specific code
    int match = sUriMatcher.match(uri);
    switch (match) {
        case EVENTS:

            cursor = database.query(EventEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
            break;
        case EVENT_ID:

            selection = EventEntry._ID + "=?";
            selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};

            cursor = database.query(EventEntry.TABLE_NAME, projection, selection, selectionArgs,
                    null, null, sortOrder);
            break;
        default:
            throw new IllegalArgumentException("Cannot query unknown URI " + uri);
    }
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}

合同

public final class EventContract {

private EventContract (){}

public static final String CONTENT_AUTHORITY = "com.example.deadline";
public static final String PATH_EVENTS = "events";
public static final Uri BASE_CONTENT_URI = Uri.parse("context://" + CONTENT_AUTHORITY);

public static final class EventEntry implements BaseColumns{

    public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_EVENTS);
    public static final String CONTENT_LIST_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY
            + "/" + PATH_EVENTS;
    public final static String TABLE_NAME = "events";

    public final static String _ID = BaseColumns._ID;
    public final static String COLUMN_EVENT_NAME = "name";
    public final static String COLUMN_EVENT_DATE = "date";

    public final static String TABLE_SORT_BY = "name ASC";
}

}

0 个答案:

没有答案