下面列出了android.database.MatrixCursor#moveToFirst ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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));
}
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;
}
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;
}
@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))));
}
@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);
}
/**
* 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;
}
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;
}
@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));
}
@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));
}
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;
}
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;
}
@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)));
}
@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);
}
@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);
}
@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))));
}
private static MatrixCursor createCursor(final String[] columns) {
final MatrixCursor cursor = new MatrixCursor(columns);
cursor.addRow(new String[] { "test" });
cursor.moveToFirst();
return cursor;
}
private static MatrixCursor createCursor(final String[] columns) {
final MatrixCursor cursor = new MatrixCursor(columns);
cursor.addRow(new String[] { "default_test" });
cursor.moveToFirst();
return cursor;
}
private static MatrixCursor createCursor(final String[] columns) {
final MatrixCursor cursor = new MatrixCursor(columns);
cursor.addRow(new String[] { "default_test" });
cursor.moveToFirst();
return cursor;
}
@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;
}