public static String provide(Context context){ String phoneNumber = null; SQLiteDatabase database = DatabaseProvider.provideReadableDatabase(context); Cursor cursor = database.query(UserTable.NAME, null, // Columns - null selects all columns null, //whereClause null, //whereArgs null, // groupBy null, // having null); // orderBy if (cursor != null){ try{ if (cursor.moveToFirst()){ CursorWrapper cursorWrapper = new CursorWrapper(cursor); phoneNumber = cursorWrapper.getString( cursorWrapper.getColumnIndex(UserTable.Cols.PHONE_NUMBER)); } } finally{ cursor.close(); } } Log.d(TAG, "Phone number retrieved from DB: " + phoneNumber); return phoneNumber; }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_videoitem_list, container, false); mGridView = (GridView) rootView.findViewById(R.id.gridview); mGridView.setAdapter(new MovieAdapter(getActivity())); mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { CursorWrapper c = (CursorWrapper) parent.getAdapter().getItem(position); ((OnFragmentInteractionListener) getActivity()).onFragmentInteraction(c.getString(c.getColumnIndex(VideoItemContract.VideoItem._ID)), c.getString(c.getColumnIndex(VideoItemContract.VideoItem.CONTENT_URL))); } }); getLoaderManager().initLoader(0, null, this); return rootView; }
/** * 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; }
protected CursorWrapper closeWithCursor(final Closeable closeable, Cursor cursor) { return new CursorWrapper(cursor) { @Override public void close() { super.close(); if (closeable != null) { try { closeable.close(); } catch (IOException ignored) { } } } }; }
public ForwardVerifier() { mockCursor = mock(Cursor.class); cursorWrapper = new CursorWrapper(mockCursor); cursorMethod = new HashMap<String, Method>(); // This works because no two methods in the Cursor interface have the same name for (Method m : Cursor.class.getMethods()) { cursorMethod.put(m.getName(), m); } }
@Test public void getWrappedCursor() { Cursor mockCursor = mock(Cursor.class); CursorWrapper cursorWrapper = new CursorWrapper(mockCursor); ShadowCursorWrapper shadow = Robolectric.shadowOf(cursorWrapper); assertThat(shadow.getWrappedCursor()).isSameAs(mockCursor); }
/** Returns the first non-CursorWrapper instance contained within this object. */ public Cursor getMasterCursor() { Cursor cursor = mCursor; while (cursor instanceof CursorWrapper) { cursor = ((CursorWrapper) cursor).getWrappedCursor(); } return cursor; }
/** Returns the first FilteredCursor wrapped by the provided cursor or null if no FilteredCursor is found. */ public static FilteredCursor unwrapFilteredCursor(Cursor cursor) { while (cursor instanceof CursorWrapper) { if (cursor instanceof FilteredCursor) { return (FilteredCursor)cursor; } else { cursor = ((CursorWrapper) cursor).getWrappedCursor(); } } return null; }
@Override protected void onListItemClick(ListView l, View v, int position, long id) { Cursor c = new CursorWrapper(searchResults); c.moveToPosition(position); String path = c.getString(c.getColumnIndex(SearchResultsProvider.COLUMN_PATH)); browse(Uri.parse(path)); }
public static ShadowCursorWrapper shadowOf(CursorWrapper instance) { return (ShadowCursorWrapper) shadowOf_(instance); }
@Test @Config(manifest = Config.NONE, shadows = ShadowCursorWrapper1.class) public void getType1() { assertThat(new CursorWrapper(null).getType(expectedType)).isEqualTo(1); }
@Test @Config(manifest = Config.NONE, shadows = ShadowCursorWrapperEcho.class) public void getTypeEcho() { assertThat(new CursorWrapper(null).getType(expectedType)).isEqualTo(expectedType); }
@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); }
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; }
@Test(expected = IllegalStateException.class) public void throwsIllegalArgumentExceptionForWrongWrappedCursor() throws Exception { CursorUtil.getObjectCursor(new CursorWrapper(new CursorDummy())); }