类android.database.CursorWrapper源码实例Demo

下面列出了怎么用android.database.CursorWrapper的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: droitatedDB   文件: CursorUtil.java
/**
 * Convert a standard Android {@link Cursor} into a {@link org.droitateddb.cursor.ObjectCursor}.<br>
 * This is only possible for {@link Cursor}s that where queried over a {@link ContentResolver} from a
 * {@link ContentProvider} derived by {@link BaseContentProvider}.
 *
 * @param <T>    Entity class represented within the Cursor
 * @param cursor Android {@link Cursor}
 * @return The {@link org.droitateddb.cursor.ObjectCursor} representation of the given Android {@link Cursor}
 * @throws NullPointerException     When the given cursor is null
 * @throws IllegalArgumentException When the given cursor is not of the type {@link CursorWrapper}, which will be returned by a
 *                                  {@link ContentResolver}
 * @throws IllegalStateException    When the wrapped cursor within the given cursor is not of type {@link org.droitateddb.cursor.ObjectCursor}. This indicates
 *                                  that the given cursor was not queried from a derivation {@link BaseContentProvider}
 */
@SuppressWarnings("unchecked")
public static <T> ObjectCursor<T> getObjectCursor(final Cursor cursor) {
	if (cursor == null) {
		throw new NullPointerException("The given cursor is null");
	}
	if (!(cursor instanceof CursorWrapper)) {
		throw new IllegalArgumentException(
				"The given cursor is not of type " + CursorWrapper.class.getCanonicalName() + ". It has type " + cursor.getClass().getCanonicalName() +
						". Was it queried with a ContentResolver?");
	}

	CursorWrapper wrapper = (CursorWrapper) cursor;
	Cursor wrappedCursor = wrapper.getWrappedCursor();

	if (!(wrappedCursor instanceof ObjectCursor)) {
		throw new IllegalStateException(
				"The wrapped cursor of the given CursorWrapper is not of type " + ObjectCursor.class.getCanonicalName() + ". It has type " +
						wrappedCursor.getClass().getCanonicalName() +
						". Was it queried over a ContentResolver from BaseContentProvider derived ContentProvider?");
	}
	return (ObjectCursor<T>) wrappedCursor;
}
 
源代码2 项目: droitatedDB   文件: ContentProviderTest.java
@Test
public void crudTest() {

    ContentResolver resolver = context.getContentResolver();
    Comment c1 = new Comment("asdf");
    EntityService<Comment> entityService = entityService(Comment.class);

    entityService.save(c1);
    assertThat(entityService.get()).hasSize(1);
    Uri uri = CommentContentProvider.uri(DB.CommentTable.TABLE_NAME);

    // create
    ContentValues values = new ContentValues();
    values.put("name", "aName");
    Uri itemLocation = resolver.insert(uri, values);
    assertThat("content://org.droitateddb.test.data.generated.provider.comment/comment/2").isEqualTo(itemLocation.toString());
    assertThat(entityService.get()).hasSize(2);
    assertThat(entityService.get(2).getName()).isEqualTo("aName");

    // update
    values.put("name", "otherName");
    int update = resolver.update(itemLocation, values, null, null);
    assertThat(1).isEqualTo(update);
    assertThat(entityService.get(2).getName()).isEqualTo("otherName");

    // read
    Cursor cursor = resolver.query(itemLocation, DB.CommentTable.PROJECTION, null, null, null);
    assertThat(cursor).isNotNull();
    // Wrap to unwrap ... this is a bit strange, normally Android wraps the Cursor but robolectric doesn't!
    CursorWrapper wrapped = new CursorWrapper(cursor);
    ObjectCursor<Comment> objectCursor = CursorUtil.getObjectCursor(wrapped);

    assertThat(objectCursor.size()).isEqualTo(1);
    Comment curserloadedObject = objectCursor.getOne();
    assertThat(curserloadedObject.getName()).isEqualTo("otherName");
    assertSameFields(curserloadedObject, entityService.get(curserloadedObject.getId()));

    // read all
    Cursor nextCursor = resolver.query(uri, DB.CommentTable.PROJECTION, null, null, null);
    assertThat(nextCursor).isNotNull();
    // Wrap to unwrap ... this is a bit strange, normally Android wraps the Cursor but robolectric doesn't!
    CursorWrapper nextWrapped = new CursorWrapper(nextCursor);
    ObjectCursor<Comment> allObjectCursor = CursorUtil.getObjectCursor(nextWrapped);

    assertThat(allObjectCursor.size()).isEqualTo(2);

    // delete
    int delete = resolver.delete(itemLocation, null, null);
    assertThat(entityService.get()).hasSize(1);
    assertThat(1).isEqualTo(delete);

}
 
源代码3 项目: droitatedDB   文件: ContentProviderTest.java
private Cursor getSingleCursor() {
    Cursor cursor = context.getContentResolver().query(CommentContentProvider.uri(DB.SingleTable.TABLE_NAME), DB.SingleTable.PROJECTION, null, null, null);
    // Wrap to unwrap ... this is a bit strange, normally Android wraps the Cursor but robolectric doesn't!
    CursorWrapper wrapped = new CursorWrapper(cursor);
    return wrapped;
}
 
源代码4 项目: droitatedDB   文件: CursorUtilTest.java
@Test(expected = IllegalStateException.class)
public void throwsIllegalArgumentExceptionForWrongWrappedCursor() throws Exception {
	CursorUtil.getObjectCursor(new CursorWrapper(new CursorDummy()));
}