List of usage examples for android.content ContentValues ContentValues
public ContentValues()
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. *//* w w w.jav a 2 s. 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.liferay.alerts.database.DatabaseHelper.java
private void _convertMessageToJSONObject(SQLiteDatabase database) { StringBuilder select = new StringBuilder(); select.append("SELECT "); select.append(Alert.ID);//from www . ja v a2 s . co m select.append(", "); select.append(Alert.PAYLOAD); select.append(" FROM "); select.append(AlertDAO.TABLE_NAME); Cursor cursor = database.rawQuery(select.toString(), null); while (cursor.moveToNext()) { try { long id = cursor.getLong(cursor.getColumnIndex(Alert.ID)); String payload = cursor.getString(cursor.getColumnIndex(Alert.PAYLOAD)); JSONObject jsonObject = new JSONObject(); jsonObject.put(Alert.MESSAGE, payload); ContentValues values = new ContentValues(); values.put(Alert.PAYLOAD, jsonObject.toString()); StringBuilder sb = new StringBuilder(); sb.append(Alert.ID); sb.append(CharPool.SPACE); sb.append("= ?"); String whereClause = sb.toString(); String[] whereArgs = { String.valueOf(id) }; database.update(AlertDAO.TABLE_NAME, values, whereClause, whereArgs); } catch (JSONException je) { Log.e(_TAG, "Couldn't convert message.", je); } } }
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 ww w .ja v a 2 s. com*/ } ContentValues val = new ContentValues(); val.put("tags", Utils.jsonToString(tags)); mCol.getDb().update("col", val); mChanged = false; } }
From source file:com.example.riteden.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// w w w .j a va 2 s.c om * @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 //WeatherProvider.query() Cursor cur = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI, new String[] { WeatherContract.LocationEntry._ID }, WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting }, null); if (cur.moveToFirst()) { return cur.getLong(cur.getColumnIndex(WeatherContract.LocationEntry._ID)); } ContentValues values = new ContentValues(); values.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting); values.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName); values.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat); values.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon); Uri return_rowID = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, values); // If it exists, return the current ID // Otherwise, insert it using the content resolver and the base URI return ContentUris.parseId(return_rowID); }
From source file:com.luxtech_eg.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/*from ww w . j a v a2s .co 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) { 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_LATITUDEE, lat); locationValues.put(WeatherContract.LocationEntry.COLUMN_LONGITUDE, 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:net.peterkuterna.android.apps.devoxxsched.service.NewsSyncService.java
@Override protected void doSync(Intent intent) throws Exception { Log.d(TAG, "Start news sync"); final Context context = this; final SharedPreferences settings = Prefs.get(context); final int localVersion = settings.getInt(DevoxxPrefs.NEWS_LOCAL_VERSION, VERSION_NONE); final long startLocal = System.currentTimeMillis(); final boolean localParse = localVersion < VERSION_CURRENT; Log.d(TAG, "found localVersion=" + localVersion + " and VERSION_CURRENT=" + VERSION_CURRENT); if (localParse) { mLocalExecutor.execute(context, "cache-news.json", new NewsHandler()); // Save local parsed version settings.edit().putInt(DevoxxPrefs.NEWS_LOCAL_VERSION, VERSION_CURRENT).commit(); final ContentValues values = new ContentValues(); values.put(News.NEWS_NEW, 0);/* w ww. j a v a 2 s .c om*/ getContentResolver().update(News.CONTENT_NEW_URI, values, null, null); } Log.d(TAG, "Local sync took " + (System.currentTimeMillis() - startLocal) + "ms"); final CfpSyncManager syncManager = new CfpSyncManager(context); if (syncManager.shouldPerformRemoteSync(Intent.ACTION_SYNC.equals(intent.getAction()))) { Log.d(TAG, "Should perform remote sync"); final long startRemote = System.currentTimeMillis(); mRemoteExecutor.executeGet(NEWS_URL, new NewsHandler()); Log.d(TAG, "Remote sync took " + (System.currentTimeMillis() - startRemote) + "ms"); } else { Log.d(TAG, "Should not perform remote sync"); } final NotifierManager notifierManager = new NotifierManager(this); notifierManager.notifyNewNewsItems(); Log.d(TAG, "News sync finished"); }
From source file:com.csipsimple.utils.Columns.java
public ContentValues jsonToContentValues(JSONObject j) { ContentValues cv = new ContentValues(); for (int i = 0; i < names.length; i++) { switch (types[i]) { case STRING: j2cvString(j, cv, names[i]); break; case INT: j2cvInt(j, cv, names[i]);//from ww w .j a v a2 s .c om break; case LONG: j2cvLong(j, cv, names[i]); break; case FLOAT: j2cvFloat(j, cv, names[i]); break; case DOUBLE: j2cvDouble(j, cv, names[i]); break; case BOOLEAN: j2cvBoolean(j, cv, names[i]); } } return cv; }
From source file:com.deliciousdroid.platform.ContactManager.java
/** * Add a list of status messages to the contacts provider. * // ww w . ja va2 s .c o m * @param context the context to use * @param accountName the username of the logged in user * @param statuses the list of statuses to store */ public static void insertStatuses(Context context, String username, List<User.Status> list) { final ContentValues values = new ContentValues(); final ContentResolver resolver = context.getContentResolver(); final ArrayList<String> processedUsers = new ArrayList<String>(); final BatchOperation batchOperation = new BatchOperation(context, resolver); for (final User.Status status : list) { // Look up the user's sample SyncAdapter data row final String userName = status.getUserName(); if (!processedUsers.contains(userName)) { final long profileId = lookupProfile(resolver, userName); // Insert the activity into the stream if (profileId > 0) { values.put(StatusUpdates.DATA_ID, profileId); values.put(StatusUpdates.STATUS, status.getStatus()); values.put(StatusUpdates.PROTOCOL, Im.PROTOCOL_CUSTOM); values.put(StatusUpdates.CUSTOM_PROTOCOL, CUSTOM_IM_PROTOCOL); values.put(StatusUpdates.IM_ACCOUNT, username); values.put(StatusUpdates.IM_HANDLE, status.getUserName()); values.put(StatusUpdates.STATUS_TIMESTAMP, status.getTimeStamp().getTime()); values.put(StatusUpdates.STATUS_RES_PACKAGE, context.getPackageName()); values.put(StatusUpdates.STATUS_ICON, R.drawable.ic_main); values.put(StatusUpdates.STATUS_LABEL, R.string.label); batchOperation.add(ContactOperations.newInsertCpo(StatusUpdates.CONTENT_URI, true) .withValues(values).build()); // A sync adapter should batch operations on multiple contacts, // because it will make a dramatic performance difference. if (batchOperation.size() >= 50) { batchOperation.execute(); } } processedUsers.add(userName); } } batchOperation.execute(); }
From source file:com.google.android.apps.santatracker.data.DestinationDbHelper.java
/** * Expects a writeable {@link SQLiteDatabase} - used for batch commits. *//*from ww w .jav a2 s . c o m*/ public void insertDestination(SQLiteDatabase db, String id, long arrivalTime, long departureTime, String city, String region, String country, double locationLat, double locationLng, long presentsDelivered, long presentsAtDestination, long timezone, long altitude, String photos, String weather, String streetView, String gmmStreetView) { ContentValues cv = new ContentValues(); cv.put(COLUMN_NAME_IDENTIFIER, id); cv.put(COLUMN_NAME_ARRIVAL, arrivalTime); cv.put(COLUMN_NAME_DEPARTURE, departureTime); cv.put(COLUMN_NAME_CITY, city); cv.put(COLUMN_NAME_REGION, region); cv.put(COLUMN_NAME_COUNTRY, country); cv.put(COLUMN_NAME_LAT, locationLat); cv.put(COLUMN_NAME_LNG, locationLng); cv.put(COLUMN_NAME_PRESENTSDELIVERED, presentsDelivered); cv.put(COLUMN_NAME_PRESENTS_DESTINATION, presentsAtDestination); cv.put(COLUMN_NAME_TIMEZONE, timezone); cv.put(COLUMN_NAME_ALTITUDE, altitude); cv.put(COLUMN_NAME_PHOTOS, photos); cv.put(COLUMN_NAME_WEATHER, weather); cv.put(COLUMN_NAME_STREETVIEW, streetView); cv.put(COLUMN_NAME_GMMSTREETVIEW, gmmStreetView); // TODO: verify whether the db parameter is needed - can we just get // another writeable handle on the db (even if the transaction is // started on a different one?) db.insertOrThrow(TABLE_NAME, null, cv); }
From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.SharedSecretObj.java
public void handleDirectMessage(Context context, Contact from, JSONObject obj) { String raw_b64;//from ww w . j a v a2s . co m try { raw_b64 = obj.getString(RAW); } catch (JSONException e) { e.printStackTrace(); return; } byte[] ss = FastBase64.decode(raw_b64); if (from.secret != null && new BigInteger(from.secret).compareTo(new BigInteger(ss)) > 0) { //ignore the new key according to a time independent metric... return; } ContentValues values = new ContentValues(); values.put(Contact.SHARED_SECRET, ss); context.getContentResolver().update(Uri.parse(DungBeetleContentProvider.CONTENT_URI + "/contacts"), values, "_id=?", new String[] { String.valueOf(from.id) }); }