Example usage for android.content ContentValues put

List of usage examples for android.content ContentValues put

Introduction

In this page you can find the example usage for android.content ContentValues put.

Prototype

public void put(String key, byte[] value) 

Source Link

Document

Adds a value to the set.

Usage

From source file:com.confidentsoftware.themebuilder.FlickrParser.java

private int addImagesFromJson(long wordId, String text, JSONObject queryResult) throws JSONException {
    JSONArray photos = queryResult.getJSONArray("photo");
    int numImages = 0;
    for (int i = 0; i < photos.length(); i++) {
        JSONObject photo = null;//from  w w  w  .ja  va  2 s.c om
        try {
            photo = photos.getJSONObject(i);
        } catch (JSONException e) {
            continue;
        }
        try {
            if (isPhotoOk(photo)) {
                String url = photo.getString(PHOTO_URL);
                String title = photo.getString("title");
                if (title.length() > MAX_TITLE_LENGTH) {
                    title = title.substring(0, MAX_TITLE_LENGTH - 3) + "...";
                }
                String id = photo.getString("id");
                Log.d(TAG, "Image: " + id);
                Uri imageUri = Images.findImage(mCr, Sites.CONTENT_URI_FLICKR, id);
                if (imageUri != null) {
                    Log.d(TAG, "Found existing image: " + imageUri);
                    // Just insert the mapping
                    Uri mappingUri = Dictionary.Words.CONTENT_URI.buildUpon().appendPath(String.valueOf(wordId))
                            .appendPath(Dictionary.PATH_IMAGES).appendPath(imageUri.getLastPathSegment())
                            .build();
                    Cursor c = mCr.query(mappingUri, PROJECTION_ID, null, null, null);
                    try {
                        if (!c.moveToNext()) {
                            mCr.insert(mappingUri, null);
                        }
                    } finally {
                        c.close();
                    }
                } else {
                    ContentValues values = new ContentValues();
                    values.put(Images.URL, url);
                    values.put(Images.SITE, ContentUris.parseId(Sites.CONTENT_URI_FLICKR));
                    values.put(Images.SITE_UNIQUE_NAME, id);
                    values.put(Images.WIDTH, photo.getInt(PHOTO_WIDTH));
                    values.put(Images.HEIGHT, photo.getInt(PHOTO_HEIGHT));
                    values.put(Images.THUMB_URL, photo.getString(THUMB_URL));
                    values.put(Images.NUM_FAVORITES, getNumFavorites(id));
                    imageUri = mCr.insert(Dictionary.BASE_CONTENT_URI.buildUpon()
                            .appendPath(Dictionary.PATH_WORDS).appendPath(String.valueOf(wordId))
                            .appendPath(Dictionary.PATH_IMAGES).build(), values);
                    //
                    // String ownerName = photo.getString("owner");
                    // Author author = findAuthor(ownerName);
                    // if (author == null) {
                    // author = buildAuthor(ownerName);
                    // }
                    // TODO set license
                    // image.setLicense(License.getLicenseByFlickrId(photo
                    // .getInt("license")));
                    // dbHelper.getImageDao().create(image);
                }
                // dbHelper.getWordImageDao().create(
                // new WordImage(word, image));
                numImages++;
            }
        } catch (JSONException e) {
            // Catch here so that we continue through the loop
            Log.w(TAG, "Error parsing photo: " + photo, e);
        }
    }
    return numImages;
}

From source file:com.hichinaschool.flashcards.libanki.Tags.java

public void flush() {
    if (mChanged) {
        JSONObject tags = new JSONObject();
        for (Map.Entry<String, Integer> t : mTags.entrySet()) {
            try {
                tags.put(t.getKey(), t.getValue());
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }/*from  w  w  w  .  j a v  a 2 s  . c  o  m*/
        }
        ContentValues val = new ContentValues();
        val.put("tags", Utils.jsonToString(tags));
        mCol.getDb().update("col", val);
        mChanged = false;
    }
}

From source file:com.fanfou.app.opensource.api.bean.User.java

public ContentValues toSimpleContentValues() {
    final User u = this;
    final ContentValues cv = new ContentValues();

    cv.put(UserInfo.SCREEN_NAME, u.screenName);
    cv.put(UserInfo.LOCATION, u.location);
    cv.put(UserInfo.GENDER, u.gender);//from  w w  w  .  jav a 2 s .  c  om
    cv.put(UserInfo.BIRTHDAY, u.birthday);

    cv.put(UserInfo.DESCRIPTION, u.description);
    cv.put(UserInfo.PROFILE_IMAGE_URL, u.profileImageUrl);
    cv.put(UserInfo.URL, u.url);
    cv.put(UserInfo.PROTECTED, u.protect);

    cv.put(UserInfo.FOLLOWERS_COUNT, u.followersCount);
    cv.put(UserInfo.FRIENDS_COUNT, u.friendsCount);
    cv.put(UserInfo.FAVORITES_COUNT, u.favouritesCount);
    cv.put(UserInfo.STATUSES_COUNT, u.statusesCount);

    cv.put(UserInfo.FOLLOWING, u.following);

    return cv;
}

From source file:com.example.fcp.sunshine.FetchWeatherTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param locationSetting The location string used to request updates from the server.
 * @param cityName        A human-readable city name, e.g "Mountain View"
 * @param lat             the latitude of the city
 * @param lon             the longitude of the city
 * @return the row ID of the added location.
 *//*from   w  w w. j  a  v  a2s  .c  o m*/
long addLocation(String locationSetting, String cityName, double lat, double lon) {
    long locationId;
    // Students: First, check if the location with this city name exists in the db
    Cursor weaCursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI, null,
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + "= ?", new String[] { locationSetting },
            null);

    // If it exists, return the current ID
    if (weaCursor.moveToFirst()) {
        int locationIdIndex = weaCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = weaCursor.getLong(locationIdIndex);
    } else {
        // Otherwise, insert it using the content resolver and the base URI
        ContentValues locValues = new ContentValues();
        locValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        locValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        locValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
        //long rowId = weaProvider.insert(WeatherContract.LocationEntry.CONTENT_URI,locValues);
        Uri retUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, locValues);
        //locationId = Long.parseLong(retUri.getLastPathSegment());
        locationId = ContentUris.parseId(retUri);
    }
    return locationId;
}

From source file:com.cloudmine.api.db.RequestDBOpenHelper.java

private ContentValues getUpdateSynchronizedContentValues(Integer newValue) {
    ContentValues updatedValues = new ContentValues();
    updatedValues.put(KEY_REQUEST_SYNCHRONIZED, newValue);
    return updatedValues;
}

From source file:edu.nd.darts.cimon.database.CimonDatabaseAdapter.java

/**
 * Insert batch of new data into Data table.
 * Batch comes as array list of {@link DataEntry} (timestamp - data pairs).
 * /*from  w w  w.j a  v  a 2s  .c o  m*/
 * @param metric       id of metric
 * @param monitor      id of monitor
 * @param data         array list of {@link DataEntry} pairs
 * @return    number of rows inserted (should equal size of _data_ array list on success)
 * 
 * @see DataTable
 */
public synchronized long insertBatchData(int metric, int monitor, ArrayList<DataEntry> data) {
    if (DebugLog.DEBUG)
        Log.d(TAG, "CimonDatabaseAdapter.insertBatchData - insert into Data table: " + "metric-" + metric);
    long rowsInserted = 0;
    ContentValues values = new ContentValues();
    values.put(DataTable.COLUMN_METRIC_ID, metric);
    values.put(DataTable.COLUMN_MONITOR_ID, monitor);

    database.beginTransaction();
    try {
        for (DataEntry entry : data) {
            ContentValues contentValues = new ContentValues(values);
            contentValues.put(DataTable.COLUMN_TIMESTAMP, entry.timestamp);
            contentValues.put(DataTable.COLUMN_VALUE, entry.value);
            if (database.insert(DataTable.TABLE_DATA, null, contentValues) >= 0) {
                rowsInserted++;
            }
        }
        // Transaction is successful and all the records have been inserted
        database.setTransactionSuccessful();
    } catch (Exception e) {
        if (DebugLog.ERROR)
            Log.e(TAG, "Error on batch insert: " + e.toString());
    } finally {
        //End the transaction
        database.endTransaction();
    }
    if (rowsInserted > 0) {
        Uri uri = Uri.withAppendedPath(CimonContentProvider.MONITOR_DATA_URI, String.valueOf(monitor));
        context.getContentResolver().notifyChange(uri, null);
    }
    return rowsInserted;
}

From source file:com.adamhurwitz.android.popularmovies.service.YouTubeService.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void putDataIntoDb(String key, String title) {

    // Create a new map of values, where column names are the keys
    ContentValues values = new ContentValues();

    // Build Movie YouTube URL
    // Construct the URL to fetch data from and make the connection.

    String youTubeUrl = YOUTUBE_BASE_URL + key;

    values.put(CursorContract.MovieData.COLUMN_NAME_YOUTUBEURL, youTubeUrl);

    Cursor c = this.getContentResolver().query(CursorContract.MovieData.CONTENT_URI,
            new String[] { CursorContract.MovieData.COLUMN_NAME_YOUTUBEURL },
            CursorContract.MovieData.COLUMN_NAME_TITLE + "= ?", new String[] { title }, // The values for the WHERE clause
            null, // don't group the rows
            null // don't filter by row groups
    );//from   w  w  w.j ava2 s .  c o m

    c.moveToFirst();

    int mRowsUpdated = this.getContentResolver().update(CursorContract.MovieData.CONTENT_URI, values,
            CursorContract.MovieData.COLUMN_NAME_TITLE + "= ?", new String[] { title });
}

From source file:com.github.gfx.android.orma.migration.SchemaDiffMigration.java

public void saveStep(@NonNull Database db, int dbVersion, @Nullable String sql, @NonNull Object... args) {
    ensureHistoryTableExists(db);//from  w ww  .j av  a  2s  .  c om

    ContentValues values = new ContentValues();
    values.put(kDbVersion, dbVersion);
    values.put(kVersionName, versionName);
    values.put(kVersionCode, versionCode);
    values.put(kSchemaHash, schemaHash);
    values.put(kSql, sql);
    values.put(kArgs, serializeArgs(args));
    db.insertOrThrow(MIGRATION_STEPS_TABLE, null, values);
}

From source file:com.waageweb.sunshine.app.FetchWeatherTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param locationSetting The location string used to request updates from the server.
 * @param cityName A human-readable city name, e.g "Mountain View"
 * @param lat the latitude of the city//from   w  ww.  j  av a 2 s.c  o m
 * @param lon the longitude of the city
 * @return the row ID of the added location.
 */
long addLocation(String locationSetting, String cityName, double lat, double lon) {
    // Students: First, check if the location with this city name exists in the db
    Cursor locationCursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting },
            null);

    if (locationCursor.moveToFirst()) {
        // If it exists, return the current ID
        return locationCursor.getLong(locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID));
    } else {
        ContentValues contentValues = new ContentValues();
        contentValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, "" + lat);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, "" + lon);

        Uri uriInserted = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                contentValues);

        // Otherwise, insert it using the content resolver and the base URI
        return ContentUris.parseId(uriInserted);
    }
}

From source file:com.android.providers.contacts.ContactsSyncAdapter.java

protected static void updateProviderImpl(String account, Long syncLocalId, Entry entry,
        ContentProvider provider) throws ParseException {
    // If this is a deleted entry then add it to the DELETED_CONTENT_URI
    ContentValues deletedValues = null;
    if (entry.isDeleted()) {
        deletedValues = new ContentValues();
        deletedValues.put(SyncConstValue._SYNC_LOCAL_ID, syncLocalId);
        final String id = entry.getId();
        final String editUri = entry.getEditUri();
        if (!TextUtils.isEmpty(id)) {
            deletedValues.put(SyncConstValue._SYNC_ID, lastItemFromUri(id));
        }//from  ww w  . ja  va2 s . co  m
        if (!TextUtils.isEmpty(editUri)) {
            deletedValues.put(SyncConstValue._SYNC_VERSION, lastItemFromUri(editUri));
        }
        deletedValues.put(SyncConstValue._SYNC_ACCOUNT, account);
    }

    if (entry instanceof ContactEntry) {
        if (deletedValues != null) {
            provider.insert(People.DELETED_CONTENT_URI, deletedValues);
            return;
        }
        updateProviderWithContactEntry(account, syncLocalId, (ContactEntry) entry, provider);
        return;
    }
    if (entry instanceof GroupEntry) {
        if (deletedValues != null) {
            provider.insert(Groups.DELETED_CONTENT_URI, deletedValues);
            return;
        }
        updateProviderWithGroupEntry(account, syncLocalId, (GroupEntry) entry, provider);
        return;
    }
    throw new IllegalArgumentException("unknown entry type, " + entry.getClass().getName());
}