Example usage for android.content ContentValues ContentValues

List of usage examples for android.content ContentValues ContentValues

Introduction

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

Prototype

public ContentValues() 

Source Link

Document

Creates an empty set of values using the default initial size

Usage

From source file:mx.com.adolfogarcia.popularmovies.data.TestUtilities.java

/**
 * Returns the values of a row that may be inserted into the movie database.
 *
 * @return the values of a row that may be inserted into the movie database.
 *//* w ww  . j  a v  a 2s.  com*/
static ContentValues createMadMaxMovieVideoValues() {
    ContentValues testValues = new ContentValues();
    testValues.put(CachedMovieVideoEntry.COLUMN_MOVIE_API_ID, 76341);
    testValues.put(CachedMovieVideoEntry.COLUMN_API_ID, "551afc679251417fd70002b1");
    testValues.put(CachedMovieVideoEntry.COLUMN_LANGUAGE, "en");
    testValues.put(CachedMovieVideoEntry.COLUMN_KEY, "jnsgdqppAYA");
    testValues.put(CachedMovieVideoEntry.COLUMN_NAME, "Trailer 2");
    testValues.put(CachedMovieVideoEntry.COLUMN_NAME, "Trailer 2");
    testValues.put(CachedMovieVideoEntry.COLUMN_SITE, "YouTube");
    testValues.put(CachedMovieVideoEntry.COLUMN_SIZE, 720);
    testValues.put(CachedMovieVideoEntry.COLUMN_TYPE, "Trailer");
    return testValues;
}

From source file:com.example.android.popularmovies.FetchMovieTask.java

private void getMovieDataFromJson(String movieJsonStr, String sortType) throws JSONException {

    //final String LOG_TAG = getMovieDataFromJson.class.getSimpleName();

    final String LOG_TAG = "getMovieDataFromJson";

    // These are the names of the JSON objects that need to be extracted.
    final String MDB_RESULTS = "results";
    final String MDB_POPULARITY = "popularity";
    final String MDB_ORIGINAL_TITLE = "original_title";
    final String MDB_POSTER_PATH_THUMBNAIL = "poster_path";
    final String MDB_PLOT_SYNOPSIS = "overview";
    final String MDB_USER_RATING = "vote_average";
    final String MDB_RELEASE_DATE = "release_date";
    final String MDB_ID = "id";

    try {/* w w w  . ja va  2s.c  o m*/
        JSONObject movieJson = new JSONObject(movieJsonStr);
        JSONArray movieArray = movieJson.getJSONArray(MDB_RESULTS);

        // Insert the new  movie information into the database
        Vector<ContentValues> cVVector = new Vector<ContentValues>(movieArray.length());

        for (int i = 0; i < movieArray.length(); i++) {
            // Get the JSON object representing the movie
            JSONObject singleMovie = movieArray.getJSONObject(i);

            ContentValues movieValues = new ContentValues();

            String yy = mContext.getString(R.string.pref_sort_favourite);

            if (sortType.equals(mContext.getString(R.string.pref_sort_popular))) {
                Log.v(LOG_TAG, "Sort: Order Popular");

                movieValues.put(MovieListEntry.COLUMN_MOVIE_ID, singleMovie.getString(MDB_ID));
                movieValues.put(MovieListEntry.COLUMN_POPULARITY, singleMovie.getInt(MDB_POPULARITY));
                movieValues.put(MovieListEntry.COLUMN_ORIGINAL_TITLE,
                        singleMovie.getString(MDB_ORIGINAL_TITLE));
                movieValues.put(MovieListEntry.COLUMN_PLOT_SYNOPSIS, singleMovie.getString(MDB_PLOT_SYNOPSIS));
                movieValues.put(MovieListEntry.COLUMN_USER_RATING, singleMovie.getDouble(MDB_USER_RATING));
                movieValues.put(MovieListEntry.COLUMN_RELEASE_DATE, singleMovie.getString(MDB_RELEASE_DATE));
                movieValues.put(MovieListEntry.COLUMN_POSTER_PATH_THUMBNAIL,
                        singleMovie.getString(MDB_POSTER_PATH_THUMBNAIL));

            } else if (sortType.equals(mContext.getString(R.string.pref_sort_highestrated))) {
                Log.v(LOG_TAG, "Sort: Order Highest Rated");
                movieValues.put(HighestRatedMovies.COLUMN_MOVIE_ID, singleMovie.getString(MDB_ID));
                movieValues.put(HighestRatedMovies.COLUMN_POPULARITY, singleMovie.getInt(MDB_POPULARITY));
                movieValues.put(HighestRatedMovies.COLUMN_ORIGINAL_TITLE,
                        singleMovie.getString(MDB_ORIGINAL_TITLE));
                movieValues.put(HighestRatedMovies.COLUMN_PLOT_SYNOPSIS,
                        singleMovie.getString(MDB_PLOT_SYNOPSIS));
                movieValues.put(HighestRatedMovies.COLUMN_USER_RATING, singleMovie.getDouble(MDB_USER_RATING));
                movieValues.put(HighestRatedMovies.COLUMN_RELEASE_DATE,
                        singleMovie.getString(MDB_RELEASE_DATE));
                movieValues.put(HighestRatedMovies.COLUMN_POSTER_PATH_THUMBNAIL,
                        singleMovie.getString(MDB_POSTER_PATH_THUMBNAIL));

            } else if (sortType.equals(mContext.getString(R.string.pref_sort_favourite))) {
                Log.v(LOG_TAG, "Sort: Order Favorite");
                movieValues.put(MovieListEntry.COLUMN_MOVIE_ID, singleMovie.getString(MDB_ID));
                movieValues.put(MovieListEntry.COLUMN_POPULARITY, singleMovie.getInt(MDB_POPULARITY));
                movieValues.put(MovieListEntry.COLUMN_ORIGINAL_TITLE,
                        singleMovie.getString(MDB_ORIGINAL_TITLE));
                movieValues.put(MovieListEntry.COLUMN_PLOT_SYNOPSIS, singleMovie.getString(MDB_PLOT_SYNOPSIS));
                movieValues.put(MovieListEntry.COLUMN_USER_RATING, singleMovie.getDouble(MDB_USER_RATING));
                movieValues.put(MovieListEntry.COLUMN_RELEASE_DATE, singleMovie.getString(MDB_RELEASE_DATE));
                movieValues.put(MovieListEntry.COLUMN_POSTER_PATH_THUMBNAIL,
                        singleMovie.getString(MDB_POSTER_PATH_THUMBNAIL));

                //Default oto Highest Rated Movie view if sort order preference is favorite and no movie is marked as favorite
                // Display Message to user

            } else {
                Log.d(LOG_TAG, "Sort Order Not Found:" + sortType);
            }

            cVVector.add(movieValues);
        }
        int inserted = 0;

        // add to database
        if (cVVector.size() > 0) {
            ContentValues[] cvArray = new ContentValues[cVVector.size()];
            cVVector.toArray(cvArray);
            if (sortType.equals(mContext.getString(R.string.pref_sort_popular))) {
                inserted = mContext.getContentResolver().bulkInsert(MovieListEntry.CONTENT_URI, cvArray);
            } else if (sortType.equals(mContext.getString(R.string.pref_sort_highestrated))) {
                inserted = mContext.getContentResolver().bulkInsert(HighestRatedMovies.CONTENT_URI, cvArray);
            }
        }

        // Log.d(LOG_TAG, "FetchMovieTask Complete. " + cVVector.size() + " Inserted");
        Log.d(LOG_TAG, "FetchMovieTask Complete. " + inserted + " Inserted");

    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        e.printStackTrace();
    }

}

From source file:com.example.cmput301.model.DatabaseManager.java

/**
 * Post a task to the "remote" table of the database.
 *
 * @param task The task to be added./*ww w  . j a  v  a 2  s . co  m*/
 * @return The task that was added along with it's id.
 */
public Task postRemote(Task task) {
    ContentValues cv = new ContentValues();
    cv.put(StringRes.COL_ID, task.getId());
    try {
        cv.put(StringRes.COL_CONTENT, task.toJson().toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    db.insert(StringRes.REMOTE_TASK_TABLE_NAME, StringRes.COL_ID, cv);
    return task;
}

From source file:com.example.android.threadsample.RSSPullParser.java

/**
 * This method parses XML in an input stream and stores parts of the data in memory
 *
 * @param inputStream a stream of data containing XML elements, usually a RSS feed
 * @param progressNotifier a helper class for sending status and logs
 * @throws XmlPullParserException defined by XMLPullParser; thrown if the thread is cancelled.
 * @throws IOException thrown if an IO error occurs during parsing
 * @throws JSONException /*www  .  j  a  v a 2s .c om*/
 */
public void parseXml(InputStream inputStream, BroadcastNotifier progressNotifier)
        throws XmlPullParserException, IOException, JSONException {

    String jsonString = "";
    byte[] buffer = new byte[1024 * 20];
    int result = inputStream.read(buffer);
    while (result > 0) {
        byte[] buf = new byte[result];
        System.arraycopy(buffer, 0, buf, 0, result);
        jsonString += new String(buf);
        result = inputStream.read(buffer);
    }

    JSONObject jsonObject = new JSONObject(jsonString);
    JSONArray jsonArray = jsonObject.getJSONArray("data");

    // Sets the number of images read to 1
    int imageCount = 1;

    // Creates a new store for image URL data
    mImages = new Vector<ContentValues>(VECTOR_INITIAL_SIZE);

    int i = 0;
    // Loops indefinitely. The exit occurs if there are no more URLs to process
    while (i < jsonArray.length()) {
        jsonObject = jsonArray.getJSONObject(i);
        String standardResolutionImageUrl = jsonObject.getJSONObject("images")
                .getJSONObject("standard_resolution").getString("url");
        String lowResolutionImageUrl = jsonObject.getJSONObject("images").getJSONObject("low_resolution")
                .getString("url");

        mImage = new ContentValues();
        String imageUrlKey;
        String imageNameKey;
        String fileName;
        imageUrlKey = DataProviderContract.IMAGE_URL_COLUMN;
        imageNameKey = DataProviderContract.IMAGE_PICTURENAME_COLUMN;
        fileName = Uri.parse(standardResolutionImageUrl).getLastPathSegment();
        mImage.put(imageUrlKey, standardResolutionImageUrl);
        mImage.put(imageNameKey, fileName);

        imageUrlKey = DataProviderContract.IMAGE_THUMBURL_COLUMN;
        imageNameKey = DataProviderContract.IMAGE_THUMBNAME_COLUMN;
        fileName = Uri.parse(lowResolutionImageUrl).getLastPathSegment();
        mImage.put(imageUrlKey, lowResolutionImageUrl);
        mImage.put(imageNameKey, fileName);

        // Logs progress
        progressNotifier.notifyProgress("Parsed Image[" + imageCount + "]:"
                + mImage.getAsString(DataProviderContract.IMAGE_URL_COLUMN));

        // Adds the current ContentValues to the ContentValues storage
        mImages.add(mImage);

        // Increments the count of the number of images stored.
        imageCount++;

        // Clears out the current ContentValues
        mImage = null;

        i++;
    }
}

From source file:com.example.asaldanha.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 w  w .j  a  va  2 s .c  o m
 * @param lon the longitude of the city
 * @return the row ID of the added location.
 */
public 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
    // If it exists, return the current ID
    // Otherwise, insert it using the content resolver and the base URI

    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()) {
        int locationIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationID = locationCursor.getLong(locationIndex);
    } else {

        //Create a new set of content values
        ContentValues locationValues = new ContentValues();

        locationValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);

        Uri insertURI = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                locationValues);
        locationID = ContentUris.parseId(insertURI);
    }
    return locationID;
}

From source file:edu.msu.walajahi.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 a2s. 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
    // If it exists, return the current ID
    // Otherwise, insert it using the content resolver and the base URI

    long locationId;

    // 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()) {
        int locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = locationCursor.getLong(locationIdIndex);
    } else {
        // Now that the content provider is set up, inserting rows of data is pretty simple.
        // First create a ContentValues object to hold the data you want to insert.
        ContentValues locationValues = new ContentValues();

        // Then add the data, along with the corresponding name of the data type,
        // so the content provider knows what kind of value is being inserted.
        locationValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);

        // Finally, insert location data into the database.
        Uri insertedUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                locationValues);

        // The resulting URI contains the ID for the row.  Extract the locationId from the Uri.
        locationId = ContentUris.parseId(insertedUri);
    }

    locationCursor.close();
    // Wait, that worked?  Yes!
    return locationId;
}

From source file:com.vrj.udacity.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// ww  w.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
    // If it exists, return the current ID
    // Otherwise, insert it using the content resolver and the base URI
    long retID = -1L;

    Cursor cursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI, // location db
            new String[] { WeatherContract.LocationEntry._ID }, // We want the ID column  
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + "= ?", // Must add = ? param or will not match up
            new String[] { locationSetting }, null);

    if (true == cursor.moveToFirst()) { // If we got something back look for id
        // retID = cursor.getLong(0);       // Return the ID
        int location_index = cursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        retID = cursor.getLong(location_index);
    } else { // Do an insert of the parameter values

        // Creation of ContentValues for insert
        // TODO: Fixup, order is different, if causes trouble
        ContentValues cValues = new ContentValues();
        cValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        cValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        cValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        cValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);

        // Insert new location in db
        Uri insertedUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                cValues);

        // Extract the locationId from the Uri you got back from .insert().
        retID = ContentUris.parseId(insertedUri);

    }

    cursor.close(); // REMEMEBER to close your cursors.

    return retID;
}

From source file:com.example.conor.dotaapp.FetchMatchTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param steamAccountId Steam account id of the player.
 * @param heroId id of the player's hero
 * @param playerSlot the position of the player on the team
 * @return the row ID of the added location.
 *//*  w  w w  . jav a  2s .  co  m*/
long addPlayer(String steamAccountId, int heroId, int playerSlot) {
    long steamId;

    // First, check if the location with this city name exists in the db
    Cursor playerCursor = mContext.getContentResolver().query(MatchContract.PlayerEntry.CONTENT_URI,
            new String[] { MatchContract.PlayerEntry._ID },
            MatchContract.PlayerEntry.COLUMN_ACCOUNT_ID + " = ?", new String[] { steamAccountId }, null);

    if (playerCursor.moveToFirst()) {
        int steamIdIndex = playerCursor.getColumnIndex(MatchContract.PlayerEntry._ID);
        steamId = playerCursor.getLong(steamIdIndex);
    } else {
        // Now that the content provider is set up, inserting rows of data is pretty simple.
        // First create a ContentValues object to hold the data you want to insert.
        ContentValues playerValues = new ContentValues();

        // Then add the data, along with the corresponding name of the data type,
        // so the content provider knows what kind of value is being inserted.
        playerValues.put(MatchContract.PlayerEntry.COLUMN_ACCOUNT_ID, steamAccountId);
        playerValues.put(MatchContract.PlayerEntry.COLUMN_HERO_ID, heroId);
        playerValues.put(MatchContract.PlayerEntry.COLUMN_P_SLOT, playerSlot);

        // Finally, insert location data into the database.
        Uri insertedUri = mContext.getContentResolver().insert(MatchContract.PlayerEntry.CONTENT_URI,
                playerValues);

        // The resulting URI contains the ID for the row.  Extract the locationId from the Uri.
        steamId = ContentUris.parseId(insertedUri);
    }

    playerCursor.close();
    return steamId;
}

From source file:com.bellman.bible.service.db.bookmark.BookmarkDBAdapter.java

public LabelDto insertLabel(LabelDto label) {
    // Create a new row of values to insert.
    ContentValues newValues = new ContentValues();
    newValues.put(LabelColumn.NAME, label.getName());

    long newId = db.insert(Table.LABEL, null, newValues);
    LabelDto newLabel = getLabelDto(newId);
    return newLabel;
}

From source file:app.com.example.android.sunshine.FetchWeatherTask.java

long addLocation(String locationSetting, String cityName, double lat, double lon) {
    // Students: First, check if the location with this city name exists in the db
    // If it exists, return the current ID
    // Otherwise, insert it using the content resolver and the base URI
    long locationId;
    SQLiteDatabase db = new WeatherDbHelper(this.mContext).getWritableDatabase();
    Cursor locationCursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " =? ", new String[] { locationSetting },
            null);// ww  w.  ja  va  2  s.  c  om
    if (locationCursor.moveToFirst()) {
        int locationIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = locationCursor.getLong(locationIndex);
    } else {
        ContentValues contentValues = new ContentValues();
        contentValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
        Uri insertedUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                contentValues);
        locationId = ContentUris.parseId(insertedUri);
    }
    locationCursor.close();
    return locationId;
}