Java Code Examples for android.database.MatrixCursor#moveToFirst()

The following examples show how to use android.database.MatrixCursor#moveToFirst() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GetFunctionTest.java    From microorm with Apache License 2.0 6 votes vote down vote up
@Test
public void functionShouldReturnTheSameObjectsAsListFromCursor() throws Exception {
  MatrixCursor cursor = new MatrixCursor(new String[] { SIMPLE_ENTITY_COLUMN });
  for (int i = 0; i != 5; i++) {
    cursor.addRow(new Object[] { "row" + i });
  }

  List<SimpleEntity> reference = testSubject.listFromCursor(cursor, SimpleEntity.class);

  List<SimpleEntity> fromFunction = Lists.newArrayList();
  Function<Cursor, SimpleEntity> function = testSubject.getFunctionFor(SimpleEntity.class);
  cursor.moveToFirst();
  do {
    fromFunction.add(function.apply(cursor));
  } while (cursor.moveToNext());

  assertThat(fromFunction).containsSequence(reference);
}
 
Example 2
Source File: ChannelTest.java    From xipl with Apache License 2.0 5 votes vote down vote up
private static MatrixCursor getChannelCursor(ContentValues contentValues) {
    String[] rows = Channel.PROJECTION;
    MatrixCursor cursor = new MatrixCursor(rows);
    MatrixCursor.RowBuilder builder = cursor.newRow();
    for(String row: rows) {
        if (row != null) {
            builder.add(row, contentValues.get(row));
        }
    }
    cursor.moveToFirst();
    return cursor;
}
 
Example 3
Source File: ContainsValuesTest.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Test
public void test()
{
    ContentValues values = new ContentValues();
    values.put("a", 123);
    values.put("b", "stringValue");
    values.put("c", new byte[] { 3, 2, 1 });
    values.putNull("d");

    MatrixCursor cursor = new MatrixCursor(new String[] { "c", "b", "a", "d", "f" });
    cursor.addRow(new Seq<>(new byte[] { 3, 2, 1 }, "stringValue", 123, null, "xyz"));
    cursor.addRow(new Seq<>(new byte[] { 3, 2, 1 }, "stringValue", "123", null, "xyz"));
    cursor.addRow(new Seq<>(new byte[] { 3, 2 }, "stringValue", 123, null, "xyz"));
    cursor.addRow(new Seq<>(new byte[] { 3, 2, 1 }, "stringValueX", 123, null, "xyz"));
    cursor.addRow(new Seq<>(new byte[] { 3, 2, 1 }, "stringValue", 1234, null, "xyz"));
    cursor.addRow(new Seq<>(new byte[] { 3, 2, 1 }, "stringValue", 123, "123", "xyz"));
    cursor.addRow(new Seq<>(321, "stringValueX", "1234", "123", "xyz"));
    cursor.addRow(new Seq<>(new byte[] { 3, 2, 1, 0 }, "stringValueX", 1234, "123", "xyz"));

    cursor.moveToFirst();
    assertThat(new ContainsValues(values), is(satisfiedBy(cursor)));
    cursor.moveToNext();
    assertThat(new ContainsValues(values), is(satisfiedBy(cursor)));
    cursor.moveToNext();
    assertThat(new ContainsValues(values), is(not(satisfiedBy(cursor))));
    cursor.moveToNext();
    assertThat(new ContainsValues(values), is(not(satisfiedBy(cursor))));
    cursor.moveToNext();
    assertThat(new ContainsValues(values), is(not(satisfiedBy(cursor))));
    cursor.moveToNext();
    assertThat(new ContainsValues(values), is(not(satisfiedBy(cursor))));
    cursor.moveToNext();
    assertThat(new ContainsValues(values), is(not(satisfiedBy(cursor))));
    cursor.moveToNext();
    assertThat(new ContainsValues(values), is(not(satisfiedBy(cursor))));
}
 
Example 4
Source File: CursorContentValuesInstanceAdapter.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Override
public TaskAdapter taskAdapter()
{
    // make sure we remove any instance fields
    ContentValues values = new Reduced<String, ContentValues>(
            () -> new ContentValues(mValues),
            (contentValues, column) -> {
                contentValues.remove(column);
                return contentValues;
            },
            INSTANCE_COLUMN_NAMES).value();

    // create a new cursor which doesn't contain the instance columns
    String[] cursorColumns = new Collected<>(
            ArrayList::new,
            new Sieved<>(col -> !INSTANCE_COLUMN_NAMES.contains(col), new Seq<>(mCursor.getColumnNames())))
            .value().toArray(new String[0]);
    MatrixCursor cursor = new MatrixCursor(cursorColumns);
    cursor.addRow(
            new Mapped<>(
                    column -> mCursor.getType(column) == Cursor.FIELD_TYPE_BLOB ? mCursor.getBlob(column) : mCursor.getString(column),
                    new Mapped<>(
                            mCursor::getColumnIndex,
                            new Seq<>(cursorColumns))));
    cursor.moveToFirst();
    return new CursorContentValuesTaskAdapter(valueOf(InstanceAdapter.TASK_ID), cursor, values);
}
 
Example 5
Source File: AutoCompleteAdapterTest.java    From open with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void bindView_shouldSetPinIconForAutoCompleteResults() throws Exception {
    MatrixCursor cursor = (MatrixCursor) adapter.getCursor();
    cursor.moveToFirst();
    SimpleFeature simpleFeature = new SimpleFeature();
    simpleFeature.setProperty(TEXT, "New York, NY");
    byte[] data = ParcelableUtil.marshall(simpleFeature);
    cursor.addRow(new Object[]{0, data});
    TextView textView = new TextView(application);
    adapter.bindView(textView, application, cursor);

    Drawable expected = app.getResources().getDrawable(R.drawable.ic_pin_outline);
    Drawable actual = textView.getCompoundDrawables()[0];
    assertDrawable(expected, actual);
}
 
Example 6
Source File: DateSerializerTest.java    From sprinkles with Apache License 2.0 5 votes vote down vote up
@Test
public void serialize() {
    DateSerializer serializer = new DateSerializer();
    String name = "date";

    Date obj = new Date(100);
    ContentValues cv = new ContentValues();
    serializer.pack(obj, cv, name);

    MatrixCursor c = new MatrixCursor(new String[]{name});
    c.addRow(new Object[]{cv.getAsLong(name)});

    c.moveToFirst();
    assertTrue(serializer.unpack(c, name).equals(new Date(100)));
}
 
Example 7
Source File: ChannelTest.java    From androidtv-sample-inputs with Apache License 2.0 5 votes vote down vote up
private static MatrixCursor getChannelCursor(ContentValues contentValues) {
    String[] rows = Channel.PROJECTION;
    MatrixCursor cursor = new MatrixCursor(rows);
    MatrixCursor.RowBuilder builder = cursor.newRow();
    for(String row: rows) {
        if (row != null) {
            builder.add(row, contentValues.get(row));
        }
    }
    cursor.moveToFirst();
    return cursor;
}
 
Example 8
Source File: ProgramTest.java    From androidtv-sample-inputs with Apache License 2.0 5 votes vote down vote up
private static MatrixCursor getProgramCursor(ContentValues contentValues) {
    String[] rows = Program.PROJECTION;
    MatrixCursor cursor = new MatrixCursor(rows);
    MatrixCursor.RowBuilder builder = cursor.newRow();
    for(String row: rows) {
        if (row != null) {
            builder.add(row, contentValues.get(row));
        }
    }
    cursor.moveToFirst();
    return cursor;
}
 
Example 9
Source File: DoubleSerializerTest.java    From sprinkles with Apache License 2.0 5 votes vote down vote up
@Test
public void serialize() {
    DoubleSerializer serializer = new DoubleSerializer();
    String name = "double";

    Double obj = 1d;
    ContentValues cv = new ContentValues();
    serializer.pack(obj, cv, name);

    MatrixCursor c = new MatrixCursor(new String[]{name});
    c.addRow(new Object[]{cv.getAsDouble(name)});

    c.moveToFirst();
    assertTrue(serializer.unpack(c, name).equals(1d));
}
 
Example 10
Source File: BooleanSerializerTest.java    From sprinkles with Apache License 2.0 5 votes vote down vote up
@Test
public void serialize() {
    BooleanSerializer serializer = new BooleanSerializer();
    String name = "boolean";

    Boolean obj = true;
    ContentValues cv = new ContentValues();
    serializer.pack(obj, cv, name);

    MatrixCursor c = new MatrixCursor(new String[]{name});
    c.addRow(new Object[]{cv.getAsInteger(name)});

    c.moveToFirst();
    assertTrue(serializer.unpack(c, name));
}
 
Example 11
Source File: TemplateProvider.java    From Dashboard with MIT License 5 votes vote down vote up
public Cursor query(Uri paramUri, String[] paramArrayOfString1, String paramString1, String[] paramArrayOfString2, String paramString2) {
    MatrixCursor cursor = new MatrixCursor(new String[]{"string"});
    try {
        final String path = paramUri.getPath().substring(1);
        for (String s : getContext().getAssets().list(path)) {
            cursor.newRow().add(s);
            cursor.moveToNext();
        }
        cursor.moveToFirst();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cursor;
}
 
Example 12
Source File: PadViewActivity.java    From padland with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a padData object trying to complete the empty fields the others.
 * Possible combinations is like this: padName + padServer = padUrl
 *
 * @param padName
 * @param padServer
 * @param padUrl
 * @return
 */
public Pad makePadData(String padName, String padLocalName, String padServer, String padUrl) {
    if (padUrl == null || padUrl.isEmpty()) {
        if (padName == null || padName.isEmpty()) {
            return null;
        }
        if (padLocalName == null || padLocalName.isEmpty()) {
            padLocalName = padName;
        }
        if (padUrl == null || padUrl.isEmpty()) {
            padUrl = padServer + padName;
        }
        if (padServer == null || padServer.isEmpty()) {
            padServer = padUrl.replace(padName, "");
        }
    } else if (padName == null && padServer == null) {
        padName = padUrl.substring(padUrl.lastIndexOf("/") + 1);
        padServer = padUrl.substring(0, padUrl.lastIndexOf("/"));
    } else if (padName.isEmpty()) {
        padName = padUrl.replace(padServer, "");
    } else if (padServer.isEmpty()) {
        padServer = padUrl.replace(padName, "");
    }

    String[] columns = PadContentProvider.getPadFieldsList();

    // This creates a fake cursor
    MatrixCursor matrixCursor = new MatrixCursor(columns);
    startManagingCursor(matrixCursor);
    matrixCursor.addRow(new Object[]{0, padName, padLocalName, padServer, padUrl, 0, 0, 0});

    matrixCursor.moveToFirst();
    Pad PadData = new Pad(matrixCursor);
    matrixCursor.close();

    return PadData;
}
 
Example 13
Source File: SystemInfoDatabaseMappingShould.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void map_cursor_to_object() {
    String[] columnsWithId = CollectionsHelper.appendInNewArray(SystemInfoTableInfo.TABLE_INFO.columns().all(),
            SystemInfoTableInfo.Columns.ID);
    MatrixCursor cursor = new MatrixCursor(columnsWithId);

    cursor.addRow(new Object[]{dateString, systemInfo.dateFormat(),
            systemInfo.version(), systemInfo.contextPath(), systemInfo.systemName(), systemInfo.id()});
    cursor.moveToFirst();

    SystemInfo dbSystemInfo = SystemInfo.create(cursor);
    cursor.close();

    assertThat(dbSystemInfo).isEqualTo(systemInfo);
}
 
Example 14
Source File: ContainsValuesTest.java    From opentasks with Apache License 2.0 5 votes vote down vote up
@Test
public void testMissingColumns()
{
    ContentValues values = new ContentValues();
    values.put("a", 123);
    values.put("b", "stringValue");
    values.put("c", new byte[] { 3, 2, 1 });
    values.putNull("d");

    MatrixCursor cursor = new MatrixCursor(new String[] { "c", "b" });
    cursor.addRow(new Seq<>(new byte[] { 3, 2, 1 }, "stringValue"));

    cursor.moveToFirst();
    assertThat(new ContainsValues(values), is(not(satisfiedBy(cursor))));
}
 
Example 15
Source File: RecordedProgramTest.java    From xipl with Apache License 2.0 5 votes vote down vote up
private static MatrixCursor getRecordedProgramCursor(ContentValues contentValues) {
    String[] rows = RecordedProgram.PROJECTION;
    MatrixCursor cursor = new MatrixCursor(rows);
    MatrixCursor.RowBuilder builder = cursor.newRow();
    for (String row : rows) {
        builder.add(row, contentValues.get(row));
    }
    cursor.moveToFirst();
    return cursor;
}
 
Example 16
Source File: FloatSerializerTest.java    From sprinkles with Apache License 2.0 5 votes vote down vote up
@Test
public void serialize() {
    FloatSerializer serializer = new FloatSerializer();
    String name = "float";

    Float obj = 1f;
    ContentValues cv = new ContentValues();
    serializer.pack(obj, cv, name);

    MatrixCursor c = new MatrixCursor(new String[]{name});
    c.addRow(new Object[]{cv.getAsFloat(name)});

    c.moveToFirst();
    assertTrue(serializer.unpack(c, name).equals(1f));
}
 
Example 17
Source File: ViewBinderTest.java    From arca-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static MatrixCursor createCursor(final String[] columns) {
	final MatrixCursor cursor = new MatrixCursor(columns);
	cursor.addRow(new String[] { "test" });
	cursor.moveToFirst();
	return cursor;
}
 
Example 18
Source File: ModernItemAdapterTest.java    From arca-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static MatrixCursor createCursor(final String[] columns) {
	final MatrixCursor cursor = new MatrixCursor(columns);
	cursor.addRow(new String[] { "default_test" });
	cursor.moveToFirst();
	return cursor;
}
 
Example 19
Source File: SupportItemAdapterTest.java    From arca-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static MatrixCursor createCursor(final String[] columns) {
	final MatrixCursor cursor = new MatrixCursor(columns);
	cursor.addRow(new String[] { "default_test" });
	cursor.moveToFirst();
	return cursor;
}
 
Example 20
Source File: CachedImageFileProvider.java    From Onosendai with Apache License 2.0 4 votes vote down vote up
@Override
public Cursor query (final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {
	LOG.i("query: %s, %s, %s, %s, %s", uri, Arrays.toString(projection), selection, selectionArgs, sortOrder);

	final Uri uriMinusExtension = removeExtension(uri);
	final MatrixCursor inputCursor = (MatrixCursor) super.query(uriMinusExtension, projection, selection, selectionArgs, sortOrder);

	// This mess to clone the single entry cursor and append file extensions to display name.
	final List<String> cols = new ArrayList<String>();
	final List<Object> values = new ArrayList<Object>();
	inputCursor.moveToFirst();
	for (final String colName : projection) {
		final int colIndex = inputCursor.getColumnIndex(colName);
		if (OpenableColumns.DISPLAY_NAME.equals(colName)) {
			if (colIndex >= 0) {
				cols.add(OpenableColumns.DISPLAY_NAME);
				values.add(makeDisplayName(inputCursor.getString(colIndex), uri));
			}
		}
		else if (OpenableColumns.SIZE.equals(colName)) {
			if (colIndex >= 0) {
				cols.add(OpenableColumns.SIZE);
				values.add(inputCursor.getLong(colIndex));
			}
		}
		else if (MediaColumns.MIME_TYPE.equals(colName)) {
			cols.add(MediaColumns.MIME_TYPE);
			values.add(getType(uri));
		}
		else if (BaseColumns._ID.equals(colName)) {
			final long id = tryReadId(uriMinusExtension);
			if (id > 0) {
				cols.add(BaseColumns._ID);
				values.add(id);
			}
		}
	}
	final String[] colsArr = cols.toArray(new String[cols.size()]);
	LOG.i("result: %s, %s", Arrays.toString(colsArr), values);
	final MatrixCursor updatedCursor = new MatrixCursor(colsArr, 1);
	updatedCursor.addRow(values);
	return updatedCursor;
}