本文整理汇总了Java中android.content.ContentValues.getAsLong方法的典型用法代码示例。如果您正苦于以下问题:Java ContentValues.getAsLong方法的具体用法?Java ContentValues.getAsLong怎么用?Java ContentValues.getAsLong使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.content.ContentValues
的用法示例。
在下文中一共展示了ContentValues.getAsLong方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fromContentValues
import android.content.ContentValues; //导入方法依赖的package包/类
static SearchRow fromContentValues(ContentValues values) {
SearchRow row = new SearchRow();
if (values.containsKey(SearchColumns.SEARCH)) {
row.mTerm = values.getAsString(SearchColumns.SEARCH);
}
if (values.containsKey(SearchColumns.DATE)) {
row.mDate = values.getAsLong(SearchColumns.DATE);
}
return row;
}
示例2: bulkInsert
import android.content.ContentValues; //导入方法依赖的package包/类
/**
* Handles requests to insert a set of new rows. In Sunshine, we are only going to be
* inserting multiple rows of data at a time from a weather forecast. There is no use case
* for inserting a single row of data into our ContentProvider, and so we are only going to
* implement bulkInsert. In a normal ContentProvider's implementation, you will probably want
* to provide proper functionality for the insert method as well.
*
* @param uri The content:// URI of the insertion request.
* @param values An array of sets of column_name/value pairs to add to the database.
* This must not be {@code null}.
*
* @return The number of values that were inserted.
*/
@Override
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
switch (sUriMatcher.match(uri)) {
case CODE_WEATHER:
db.beginTransaction();
int rowsInserted = 0;
try {
for (ContentValues value : values) {
long weatherDate =
value.getAsLong(WeatherContract.WeatherEntry.COLUMN_DATE);
if (!SunshineDateUtils.isDateNormalized(weatherDate)) {
throw new IllegalArgumentException("Date must be normalized to insert");
}
long _id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, value);
if (_id != -1) {
rowsInserted++;
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (rowsInserted > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsInserted;
default:
return super.bulkInsert(uri, values);
}
}
示例3: fromContentValues
import android.content.ContentValues; //导入方法依赖的package包/类
/**
* Create a new {@link Cheese} from the specified {@link ContentValues}.
*
* @param values A {@link ContentValues} that at least contain {@link #COLUMN_NAME}.
* @return A newly created {@link Cheese} instance.
*/
public static Cheese fromContentValues(ContentValues values) {
final Cheese cheese = new Cheese();
if (values.containsKey(COLUMN_ID)) {
cheese.id = values.getAsLong(COLUMN_ID);
}
if (values.containsKey(COLUMN_NAME)) {
cheese.name = values.getAsString(COLUMN_NAME);
}
return cheese;
}
示例4: validateExpenseAndInsert
import android.content.ContentValues; //导入方法依赖的package包/类
private Uri validateExpenseAndInsert(Uri uri, ContentValues contentValues) {
String info = contentValues.getAsString(ExpenseEntry.COLUMN_INFO).trim();
if (info.isEmpty()) {
throw new IllegalArgumentException("Expense info must be set.");
}
Double price = contentValues.getAsDouble(ExpenseEntry.COLUMN_PRICE);
if (price == null || price < 0) {
throw new IllegalArgumentException("Wrong price " + price);
}
Long vehicleId = contentValues.getAsLong(ExpenseEntry.COLUMN_VEHICLE);
String selection = VehicleEntry._ID + "=?";
String[] selectionArgs = new String[] { String.valueOf(vehicleId) };
Cursor cursor = mDbHelper.getReadableDatabase().query(VehicleEntry.TABLE_NAME, FuelUpContract.ALL_COLUMNS_VEHICLES, selection, selectionArgs, null, null, null);
if (cursor == null || cursor.getCount() != 1) {
if (cursor != null)
cursor.close();
throw new IllegalArgumentException("Vehicle with id=" + vehicleId + " does not exist.");
}
cursor.close();
SQLiteDatabase database = mDbHelper.getWritableDatabase();
long id = database.insert(ExpenseEntry.TABLE_NAME, null, contentValues);
if (id == -1) {
Log.e(LOG_TAG, "Failed to insert row for " + uri);
return null;
}
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(uri, id);
}
示例5: createFromContentValues
import android.content.ContentValues; //导入方法依赖的package包/类
/**
* Create a WordListMetadata from the contents of a ContentValues.
*
* If this lacks any required field, IllegalArgumentException is thrown.
*/
public static WordListMetadata createFromContentValues(@Nonnull final ContentValues values) {
final String id = values.getAsString(MetadataDbHelper.WORDLISTID_COLUMN);
final Integer type = values.getAsInteger(MetadataDbHelper.TYPE_COLUMN);
final String description = values.getAsString(MetadataDbHelper.DESCRIPTION_COLUMN);
final Long lastUpdate = values.getAsLong(MetadataDbHelper.DATE_COLUMN);
final Long fileSize = values.getAsLong(MetadataDbHelper.FILESIZE_COLUMN);
final String rawChecksum = values.getAsString(MetadataDbHelper.RAW_CHECKSUM_COLUMN);
final String checksum = values.getAsString(MetadataDbHelper.CHECKSUM_COLUMN);
final int retryCount = values.getAsInteger(MetadataDbHelper.RETRY_COUNT_COLUMN);
final String localFilename = values.getAsString(MetadataDbHelper.LOCAL_FILENAME_COLUMN);
final String remoteFilename = values.getAsString(MetadataDbHelper.REMOTE_FILENAME_COLUMN);
final Integer version = values.getAsInteger(MetadataDbHelper.VERSION_COLUMN);
final Integer formatVersion = values.getAsInteger(MetadataDbHelper.FORMATVERSION_COLUMN);
final Integer flags = values.getAsInteger(MetadataDbHelper.FLAGS_COLUMN);
final String locale = values.getAsString(MetadataDbHelper.LOCALE_COLUMN);
if (null == id
|| null == type
|| null == description
|| null == lastUpdate
|| null == fileSize
|| null == checksum
|| null == localFilename
|| null == remoteFilename
|| null == version
|| null == formatVersion
|| null == flags
|| null == locale) {
throw new IllegalArgumentException();
}
return new WordListMetadata(id, type, description, lastUpdate, fileSize, rawChecksum,
checksum, retryCount, localFilename, remoteFilename, version, formatVersion,
flags, locale);
}
示例6: readFromValues
import android.content.ContentValues; //导入方法依赖的package包/类
public void readFromValues(ContentValues values) {
itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
container = values.getAsLong(LauncherSettings.Favorites.CONTAINER);
screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN);
cellX = values.getAsInteger(LauncherSettings.Favorites.CELLX);
cellY = values.getAsInteger(LauncherSettings.Favorites.CELLY);
spanX = values.getAsInteger(LauncherSettings.Favorites.SPANX);
spanY = values.getAsInteger(LauncherSettings.Favorites.SPANY);
rank = values.getAsInteger(LauncherSettings.Favorites.RANK);
}
示例7: bulkInsert
import android.content.ContentValues; //导入方法依赖的package包/类
/**
* Handles requests to insert a set of new rows. In Sunshine, we are only going to be
* inserting multiple rows of data at a time from a weather forecast. There is no use case
* for inserting a single row of data into our ContentProvider, and so we are only going to
* implement bulkInsert. In a normal ContentProvider's implementation, you will probably want
* to provide proper functionality for the insert method as well.
*
* @param uri The content:// URI of the insertion request.
* @param values An array of sets of column_name/value pairs to add to the database.
* This must not be {@code null}.
*
* @return The number of values that were inserted.
*/
@Override
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
switch (sUriMatcher.match(uri)) {
// COMPLETED (2) Only perform our implementation of bulkInsert if the URI matches the CODE_WEATHER code
case CODE_WEATHER:
db.beginTransaction();
int rowsInserted = 0;
try {
for (ContentValues value : values) {
long weatherDate = value.getAsLong(WeatherContract.WeatherEntry.COLUMN_DATE);
if (!SunshineDateUtils.isDateNormalized(weatherDate)) {
throw new IllegalArgumentException("Date must be normalized to insert");
}
long _id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, value);
if (_id != 0) {
rowsInserted++;
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (rowsInserted > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
// COMPLETED (3) Return the number of rows inserted from our implementation of bulkInsert
return rowsInserted;
// COMPLETED (4) If the URI does match match CODE_WEATHER, return the super implementation of bulkInsert
default:
return super.bulkInsert(uri, values);
}
}
示例8: testDuplicateDateInsertBehaviorShouldReplace
import android.content.ContentValues; //导入方法依赖的package包/类
/**
* Tests to ensure that inserts into your database results in automatically incrementing row
* IDs and that row IDs are not reused.
* <p>
* If the INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled
* automatically with an unused integer, usually one more than the largest _ID currently in
* use. This is true regardless of whether or not the AUTOINCREMENT keyword is used.
* <p>
* If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic
* _ID assignment algorithm to prevent the reuse of _IDs over the lifetime of the database.
* In other words, the purpose of AUTOINCREMENT is to prevent the reuse of _IDs from previously
* deleted rows.
* <p>
* To test this, we first insert a row into the database and get its _ID. Then, we'll delete
* that row, change the data that we're going to insert, and insert the changed data into the
* database again. If AUTOINCREMENT isn't set up properly in the WeatherDbHelper's table
* create statement, then the _ID of the first insert will be reused. However, if AUTOINCREMENT
* is setup properly, that older ID will NOT be reused, and the test will pass.
*/
@Test
public void testDuplicateDateInsertBehaviorShouldReplace() {
/* Obtain weather values from TestUtilities */
ContentValues testWeatherValues = TestUtilities.createTestWeatherContentValues();
/*
* Get the original weather ID of the testWeatherValues to ensure we use a different
* weather ID for our next insert.
*/
long originalWeatherId = testWeatherValues.getAsLong(REFLECTED_COLUMN_WEATHER_ID);
/* Insert the ContentValues with old weather ID into database */
database.insert(
WeatherContract.WeatherEntry.TABLE_NAME,
null,
testWeatherValues);
/*
* We don't really care what this ID is, just that it is different than the original and
* that we can use it to verify our "new" weather entry has been made.
*/
long newWeatherId = originalWeatherId + 1;
testWeatherValues.put(REFLECTED_COLUMN_WEATHER_ID, newWeatherId);
/* Insert the ContentValues with new weather ID into database */
database.insert(
WeatherContract.WeatherEntry.TABLE_NAME,
null,
testWeatherValues);
/* Query for a weather record with our new weather ID */
Cursor newWeatherIdCursor = database.query(
REFLECTED_TABLE_NAME,
new String[]{REFLECTED_COLUMN_DATE},
null,
null,
null,
null,
null);
String recordWithNewIdNotFound =
"New record did not overwrite the previous record for the same date.";
assertTrue(recordWithNewIdNotFound,
newWeatherIdCursor.getCount() == 1);
/* Always close the cursor after you're done with it */
newWeatherIdCursor.close();
}
示例9: findSelfInfo
import android.content.ContentValues; //导入方法依赖的package包/类
@Override
public CallerInfo findSelfInfo(Context ctxt) {
CallerInfo callerInfo = new CallerInfo();
String[] projection = new String[] {
Profile._ID,
Profile.DISPLAY_NAME,
Profile.PHOTO_ID,
Profile.PHOTO_URI
};
Cursor cursor = ctxt.getContentResolver().query(Profile.CONTENT_URI, projection, null, null, null);
if(cursor != null) {
try {
if(cursor.getCount() > 0) {
cursor.moveToFirst();
ContentValues cv = new ContentValues();
DatabaseUtils.cursorRowToContentValues(cursor, cv);
callerInfo.contactExists = true;
if(cv.containsKey(Profile.DISPLAY_NAME) ) {
callerInfo.name = cv.getAsString(Profile.DISPLAY_NAME);
}
if(cv.containsKey(Profile._ID) ) {
callerInfo.personId = cv.getAsLong(Profile._ID);
callerInfo.contactContentUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, callerInfo.personId);
}
if(cv.containsKey(Profile.PHOTO_ID)) {
Long photoId = cv.getAsLong(Profile.PHOTO_ID);
if(photoId != null) {
callerInfo.photoId = photoId;
}
}
if(cv.containsKey(Profile.PHOTO_URI)) {
String photoUri = cv.getAsString(Profile.PHOTO_URI);
if(!TextUtils.isEmpty(photoUri)) {
callerInfo.photoUri = Uri.parse(photoUri);
}
}
if(callerInfo.name != null && callerInfo.name.length() == 0) {
callerInfo.name = null;
}
}
}catch(Exception e) {
Log.e(THIS_FILE, "Exception while retrieving cursor infos", e);
}finally {
cursor.close();
}
}
return callerInfo;
}
示例10: insertUnderTransaction
import android.content.ContentValues; //导入方法依赖的package包/类
private Uri insertUnderTransaction(SQLiteDatabase db, Uri uri, ContentValues contentValues) {
long id, noteId;
String table;
Uri resultUri;
switch (uris.matcher.match(uri)) {
case ProviderUris.LOCAL_DB_REPO:
table = DbDbRepo.TABLE;
break;
case ProviderUris.REPOS:
id = DbRepo.insert(db, contentValues.getAsString(ProviderContract.Repos.Param.REPO_URL));
return ContentUris.withAppendedId(uri, id);
case ProviderUris.BOOKS:
table = DbBook.TABLE;
long bid = db.insertOrThrow(table, null, contentValues);
insertRootNote(db, bid);
return ContentUris.withAppendedId(uri, bid);
case ProviderUris.FILTERS:
table = DbSearch.TABLE;
ProviderFilters.updateWithNextPosition(db, contentValues);
break;
case ProviderUris.NOTES:
return insertNote(db, uri, contentValues, Place.UNSPECIFIED);
case ProviderUris.NOTE_ABOVE:
return insertNote(db, uri, contentValues, Place.ABOVE);
case ProviderUris.NOTE_UNDER:
return insertNote(db, uri, contentValues, Place.UNDER);
case ProviderUris.NOTE_BELOW:
return insertNote(db, uri, contentValues, Place.BELOW);
case ProviderUris.NOTES_PROPERTIES:
noteId = contentValues.getAsLong(ProviderContract.NoteProperties.Param.NOTE_ID);
String name = contentValues.getAsString(ProviderContract.NoteProperties.Param.NAME);
String value = contentValues.getAsString(ProviderContract.NoteProperties.Param.VALUE);
int pos = contentValues.getAsInteger(ProviderContract.NoteProperties.Param.POSITION);
long nameId = DbPropertyName.getOrInsert(db, name);
long valueId = DbPropertyValue.getOrInsert(db, value);
long propertyId = DbProperty.getOrInsert(db, nameId, valueId);
long notePropertyId = DbNoteProperty.getOrInsert(db, noteId, pos, propertyId);
return ContentUris.withAppendedId(uri, notePropertyId);
case ProviderUris.LOAD_BOOK_FROM_FILE:
resultUri = loadBookFromFile(contentValues);
return resultUri;
case ProviderUris.CURRENT_ROOKS:
resultUri = insertCurrentRook(db, uri, contentValues);
return resultUri;
case ProviderUris.BOOKS_ID_SAVED:
resultUri = bookSavedToRepo(db, uri, contentValues);
return resultUri;
default:
throw new IllegalArgumentException("URI is not recognized: " + uri);
}
id = db.insertOrThrow(table, null, contentValues);
return ContentUris.withAppendedId(uri, id);
}
示例11: testIntegerAutoincrement
import android.content.ContentValues; //导入方法依赖的package包/类
/**
* Tests to ensure that inserts into your database results in automatically
* incrementing row IDs.
>>>>>>> 4174cf2... S07.02-Exercise-PreventInvalidInserts
*/
@Test
public void testIntegerAutoincrement() {
/* First, let's ensure we have some values in our table initially */
testInsertSingleRecordIntoWeatherTable();
/* Obtain weather values from TestUtilities */
ContentValues testWeatherValues = TestUtilities.createTestWeatherContentValues();
/* Get the date of the testWeatherValues to ensure we use a different date later */
long originalDate = testWeatherValues.getAsLong(REFLECTED_COLUMN_DATE);
/* Insert ContentValues into database and get a row ID back */
long firstRowId = database.insert(
REFLECTED_TABLE_NAME,
null,
testWeatherValues);
/* Delete the row we just inserted to see if the database will reuse the rowID */
database.delete(
REFLECTED_TABLE_NAME,
"_ID == " + firstRowId,
null);
/*
* Now we need to change the date associated with our test content values because the
* database policy is to replace identical dates on conflict.
*/
long dayAfterOriginalDate = originalDate + TimeUnit.DAYS.toMillis(1);
testWeatherValues.put(REFLECTED_COLUMN_DATE, dayAfterOriginalDate);
/* Insert ContentValues into database and get another row ID back */
long secondRowId = database.insert(
REFLECTED_TABLE_NAME,
null,
testWeatherValues);
String sequentialInsertsDoNotAutoIncrementId =
"IDs were reused and shouldn't be if autoincrement is setup properly.";
assertNotSame(sequentialInsertsDoNotAutoIncrementId,
firstRowId, secondRowId);
}