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:ro.weednet.contactssync.platform.ContactManager.java

public static long ensureGroupExists(Context context, Account account) {
    final ContentResolver resolver = context.getContentResolver();

    // Lookup the group
    long groupId = 0;
    final Cursor cursor = resolver.query(Groups.CONTENT_URI, new String[] { Groups._ID },
            Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=? AND " + Groups.TITLE + "=?",
            new String[] { account.name, account.type, GROUP_NAME }, null);
    if (cursor != null) {
        try {// w  w w.j  a v  a  2 s  .  c o m
            if (cursor.moveToFirst()) {
                groupId = cursor.getLong(0);
            }
        } finally {
            cursor.close();
        }
    }

    if (groupId == 0) {
        // Group doesn't exist yet, so create it
        final ContentValues contentValues = new ContentValues();
        contentValues.put(Groups.ACCOUNT_NAME, account.name);
        contentValues.put(Groups.ACCOUNT_TYPE, account.type);
        contentValues.put(Groups.TITLE, GROUP_NAME);
        //   contentValues.put(Groups.GROUP_IS_READ_ONLY, true);
        contentValues.put(Groups.GROUP_VISIBLE, true);

        final Uri newGroupUri = resolver.insert(Groups.CONTENT_URI, contentValues);
        groupId = ContentUris.parseId(newGroupUri);
    }
    return groupId;
}

From source file:com.manning.androidhacks.hack043.service.BatchService.java

@Override
protected void onHandleIntent(Intent intent) {
    Builder builder = null;//from   ww w.  j  a  v a2  s .c o  m
    ContentResolver contentResolver = getContentResolver();
    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();

    builder = ContentProviderOperation.newDelete(BatchNumbersContentProvider.CONTENT_URI);
    operations.add(builder.build());

    for (int i = 1; i <= 100; i++) {
        ContentValues cv = new ContentValues();
        cv.put(NoBatchNumbersContentProvider.COLUMN_TEXT, "" + i);

        builder = ContentProviderOperation.newInsert(BatchNumbersContentProvider.CONTENT_URI);
        builder.withValues(cv);

        operations.add(builder.build());
    }

    try {
        contentResolver.applyBatch(BatchNumbersContentProvider.AUTHORITY, operations);
    } catch (RemoteException e) {
        Log.e(TAG, "Couldn't apply batch: " + e.getMessage());
    } catch (OperationApplicationException e) {
        Log.e(TAG, "Couldn't apply batch: " + e.getMessage());
    }

}

From source file:cn.code.notes.gtask.data.SqlData.java

public SqlData(Context context, Cursor c) {
    mContentResolver = context.getContentResolver();
    mIsCreate = false;
    loadFromCursor(c);
    mDiffDataValues = new ContentValues();
}

From source file:edu.stanford.mobisocial.dungbeetle.Helpers.java

public static void deleteContact(final Context c, Long contactId) {
    /*c.getContentResolver().delete(
    Uri.parse(DungBeetleContentProvider.CONTENT_URI + "/contacts"),
    Contact._ID + "=?",/* w  w w .j a v a  2  s.  c om*/
    new String[]{ String.valueOf(contactId)});
    */

    Uri url = Uri.parse(DungBeetleContentProvider.CONTENT_URI + "/contacts");
    ContentValues values = new ContentValues();
    values.put(Contact.HIDDEN, 1);
    c.getContentResolver().update(url, values, Contact._ID + "=" + contactId, null);
}

From source file:com.bellman.bible.service.db.mynote.MyNoteDBAdapter.java

public MyNoteDto insertMyNote(MyNoteDto mynote) {
    // Create a new row of values to insert.
    Log.d(TAG, "about to insertMyNote: " + mynote.getVerseRange());
    VerseRange verseRange = mynote.getVerseRange();
    String v11nName = getVersification(verseRange);
    // Gets the current system time in milliseconds
    Long now = Long.valueOf(System.currentTimeMillis());

    ContentValues newValues = new ContentValues();
    newValues.put(MyNoteColumn.KEY, verseRange.getOsisRef());
    newValues.put(MyNoteColumn.VERSIFICATION, v11nName);
    newValues.put(MyNoteColumn.MYNOTE, mynote.getNoteText());
    newValues.put(MyNoteColumn.LAST_UPDATED_ON, now);
    newValues.put(MyNoteColumn.CREATED_ON, now);

    long newId = db.insert(Table.MYNOTE, null, newValues);
    MyNoteDto newMyNote = getMyNoteDto(newId);
    return newMyNote;
}

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

private void addTask_LocaleTable(Task task) {
    ContentValues cv = new ContentValues();
    cv.put(StringRes.COL_ID, task.getId());
    try {/*  www.  j a va2  s  .  com*/
        cv.put(StringRes.COL_CONTENT, task.toJson().toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.insert(StringRes.LOCAL_TASK_TABLE_NAME, StringRes.COL_ID, cv);
}

From source file:com.example.android.myargmenuplanner.data.LoadMenu.java

@Override
protected String[] doInBackground(String... params) {

    Uri mUri = MenuEntry.CONTENT_URI;

    Cursor mCursor = mContext.getContentResolver().query(mUri, null, null, null, null);

    if (mCursor.getCount() == 0) {

        int shift = 0;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        String sDate = df.format(cal.getTime());
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);

        Log.i(LOG_TAG, "Init Date: " + date);
        Vector<ContentValues> cVVector = new Vector<ContentValues>(14 - dayofweek);

        for (int i = dayofweek; i <= 14; i++) {

            ContentValues values = new ContentValues();

            values.put(MenuEntry.COLUMN_DATE, sDate);
            values.put(MenuEntry.COLUMN_LUNCH, "Empty");
            values.put(MenuEntry.COLUMN_ID_LUNCH, 0);
            values.put(MenuEntry.COLUMN_DINNER, "Empty");
            values.put(MenuEntry.COLUMN_ID_DINNER, 0);

            cVVector.add(values);/*from  w  w  w. j  a  v a  2 s .co  m*/

            cal.add(Calendar.DATE, 1);
            sDate = df.format(cal.getTime());
            //            Log.i(LOG_TAG, "Day of the week: "+cal.get(Calendar.DAY_OF_WEEK));
            //            Log.i(LOG_TAG, "Date: "+date);

        }

        int inserted = 0;

        //            // add to database
        Log.i(LOG_TAG, "Creando registros en base de datos. Tabla Menu ");

        if (cVVector.size() > 0) {
            ContentValues[] cvArray = new ContentValues[cVVector.size()];
            cVVector.toArray(cvArray);

            inserted = mContext.getContentResolver().bulkInsert(MenuEntry.CONTENT_URI, cvArray);
            Log.i(LOG_TAG, "Registros nuevos creados en Tabla Menu: " + inserted);
        }

    } else { //ya tengo registros, tengo que fijarme las fechas

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        String dateNow = df.format(cal.getTime());
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        String date = "";
        String week = "";
        while (mCursor.moveToNext()) {

            date = mCursor.getString(1);

            if (dateNow.equals(date)) {

            }
        }

    }

    return null;
}

From source file:com.josenaves.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  ava  2s  .co m
 * @param lon the longitude of the city
 * @return the row ID of the added location.
 */
private long addLocation(String locationSetting, String cityName, double lat, double lon) {

    // First, check if the location with this city name exists in the db
    Cursor cursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting },
            null);

    if (cursor.moveToFirst()) {
        int locationIdIndex = cursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        return cursor.getLong(locationIdIndex);
    } else {
        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_LAT, lat);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);

        Uri locationInsertUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                locationValues);

        return ContentUris.parseId(locationInsertUri);
    }
}

From source file:com.example.android.popularmoviesist2.data.FetchMovieTask.java

private void getMoviesDataFromJson(String moviesJsonStr) throws JSONException {

    final String JSON_LIST = "results";
    final String JSON_POSTER = "poster_path";
    final String JSON_TITLE = "original_title";
    final String JSON_OVERVIEW = "overview";
    final String JSON_VOTE = "vote_average";
    final String JSON_RELEASE = "release_date";
    final String JSON_ID = "id";

    try {//ww w  . j a va2s .  co  m
        JSONObject moviesJson = new JSONObject(moviesJsonStr);
        JSONArray moviesArray = moviesJson.getJSONArray(JSON_LIST);

        Vector<ContentValues> cVVector = new Vector<ContentValues>(moviesArray.length());

        for (int i = 0; i < moviesArray.length(); i++) {

            JSONObject movie = moviesArray.getJSONObject(i);
            String overview = movie.getString(JSON_OVERVIEW);
            String release = movie.getString(JSON_RELEASE);
            String poster = movie.getString(JSON_POSTER);
            String id = movie.getString(JSON_ID);
            String title = movie.getString(JSON_TITLE);
            String vote = movie.getString(JSON_VOTE);

            ContentValues movieValues = new ContentValues();

            movieValues.put(MovieEntry.COLUMN_ID, id);
            movieValues.put(MovieEntry.COLUMN_POSTER, poster);
            movieValues.put(MovieEntry.COLUMN_TITLE, title);
            movieValues.put(MovieEntry.COLUMN_OVERVIEW, overview);
            movieValues.put(MovieEntry.COLUMN_VOTE, vote);
            movieValues.put(MovieEntry.COLUMN_RELEASE, release);

            cVVector.add(movieValues);

        }

        int inserted = 0;

        //delete database
        int rowdeleted = mContext.getContentResolver().delete(MovieEntry.CONTENT_URI, null, null);

        // add to database
        if (cVVector.size() > 0) {
            ContentValues[] cvArray = new ContentValues[cVVector.size()];
            cVVector.toArray(cvArray);

            inserted = mContext.getContentResolver().bulkInsert(MovieEntry.CONTENT_URI, cvArray);
        }

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

}

From source file:cn.loveapple.client.android.database.impl.TemperatureDaoImpl.java

/**
 * /*from w  w  w.ja va  2 s  . c  o  m*/
 * {@inheritDoc}
 */
@Override
public TemperatureEntity save(TemperatureEntity source) {
    TemperatureEntity result = null;

    ContentValues values = new ContentValues();
    values.put(COLUMN_COITUS_FLG, source.getCoitusFlg());
    values.put(COLUMN_DATE, source.getDate());
    values.put(COLUMN_TIMESTAMP, source.getTimestamp());
    values.put(COLUMN_TEMPERATURE, source.getTemperature());
    values.put(COLUMN_MENSTRUATION_FLG, source.getMenstruationFlg());
    values.put(COLUMN_DYSMENORRHEA_FLG, source.getDysmenorrheaFlg());
    values.put(COLUMN_LEUKORRHEA, source.getLeukorrhea());
    values.put(COLUMN_MENSTRUATION_LEVEL, source.getMenstruationLevel());
    values.put(COLUMN_MENSTRUATION_CYCLE, source.getMenstruationCycle());
    try {
        writableDb = getWritableDb();

        int colNum = writableDb.update(TABLE_NAME, values, COLUMN_DATE + "=?",
                new String[] { source.getDate() });
        if (colNum < 1) {
            writableDb.insert(TABLE_NAME, null, values);
            Log.i(LOG_TAG, "update : " + ToStringBuilder.reflectionToString(values));
        }
        result = findByDate(source.getDate());
    } finally {
        writableDb.close();
    }

    return result;
}