List of usage examples for android.content ContentValues ContentValues
public ContentValues()
From source file:alberthsu.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 v a2s . c o 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(LocationEntry.CONTENT_URI, new String[] { LocationEntry._ID }, LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting }, null); if (cursor.moveToFirst()) { int locationIdIndex = cursor.getColumnIndex(LocationEntry._ID); return cursor.getLong(locationIdIndex); } else { ContentValues locationValues = new ContentValues(); locationValues.put(LocationEntry.COLUMN_LOCATION_SETTING, locationSetting); locationValues.put(LocationEntry.COLUMN_CITY_NAME, cityName); locationValues.put(LocationEntry.COLUMN_COORD_LAT, lat); locationValues.put(LocationEntry.COLUMN_COORD_LONG, lon); Uri locationInsertUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, locationValues); return ContentUris.parseId(locationInsertUri); } }
From source file:com.example.orangeweather.JSONWeatherParser.java
public static ContentValues getWeather2(JSONObject jObj) throws JSONException { ContentValues cv = new ContentValues(); JSONObject coordObj = getObject("coord", jObj); //cv.put(CityTags.CITY_NAME, getString("name", jObj)); cv.put("_id", getInt("id", jObj)); cv.put(WeatherTags.WEATHER_DT, getInt("dt", jObj)); //cv.put(CityTags.CITY_COORD, getDMSCoordinates(getFloat("lat", coordObj), getFloat("lon", coordObj))); JSONObject sysObj = getObject("sys", jObj); //cv.put(CityTags.CITY_SYS_COUNTRY, getString("country", sysObj)); if (sysObj.has("sunrise")) { cv.put(WeatherTags.WEATHER_SYS_SUNRISE, getInt("sunrise", sysObj)); }//from w w w .j a v a 2s .c o m if (sysObj.has("sunset")) { cv.put(WeatherTags.WEATHER_SYS_SUNSET, getInt("sunset", sysObj)); } JSONArray jArr = jObj.getJSONArray("weather"); JSONObject JSONWeather = jArr.getJSONObject(0); cv.put(WeatherTags.WEATHER_CONDITION_DESC, getString("description", JSONWeather)); cv.put(WeatherTags.WEATHER_CONDITION_MAIN, getString("main", JSONWeather)); JSONObject mainObj = getObject("main", jObj); cv.put(WeatherTags.WEATHER_MAIN_HUMIDITY, getInt("humidity", mainObj)); cv.put(WeatherTags.WEATHER_MAIN_PRESSURE, getInt("pressure", mainObj)); cv.put(WeatherTags.WEATHER_MAIN_TEMP_MAX, getTemperatureString(getFloat("temp_max", mainObj))); cv.put(WeatherTags.WEATHER_MAIN_TEMP_MIN, getTemperatureString(getFloat("temp_min", mainObj))); cv.put(WeatherTags.WEATHER_MAIN_TEMP, getTemperatureString(getFloat("temp", mainObj))); JSONObject wObj = getObject("wind", jObj); cv.put(WeatherTags.WEATHER_WIND_SPEED, getFloat("speed", wObj)); cv.put(WeatherTags.WEATHER_WIND_DEG, getInt("deg", wObj)); if (wObj.has("var_beg")) { cv.put(WeatherTags.WEATHER_WIND_VAR_BEG, getInt("var_beg", wObj)); } if (wObj.has("var_end")) { cv.put(WeatherTags.WEATHER_WIND_VAR_END, getInt("var_end", wObj)); } JSONObject cObj = getObject("clouds", jObj); cv.put(WeatherTags.WEATHER_CLOUDS_ALL, getInt("all", cObj)); return cv; }
From source file:com.example.spatidar.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 av a 2s.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 = -1; // Query if locationSetting entry exists in the table Cursor cur = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI, null, WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + "= ?", new String[] { locationSetting }, null); if (cur.moveToFirst()) { int idx = cur.getColumnIndex(WeatherContract.LocationEntry._ID); locationId = cur.getLong(idx); } else { // Insert 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 uri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, values); locationId = ContentUris.parseId(uri); } cur.close(); return locationId; }
From source file:idea.ruan.oksun.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 . jav 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) { ContentResolver contentResolver = mContext.getContentResolver(); Uri locationTableUri = WeatherContract.LocationEntry.CONTENT_URI; long locationId; Cursor c = contentResolver.query(locationTableUri, new String[] { WeatherContract.LocationEntry._ID }, WeatherContract.LocationEntry.COLUMN_CITY_NAME + " = ?", new String[] { cityName }, null); if (c.moveToFirst()) { int i = c.getColumnIndex(WeatherContract.LocationEntry._ID); locationId = c.getLong(i); } else { ContentValues cv = new ContentValues(); cv.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting); cv.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName); cv.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat); cv.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon); locationId = ContentUris.parseId(contentResolver.insert(locationTableUri, cv)); } c.close(); return locationId; }
From source file:com.example.android.myargmenuplanner.data.FetchJsonDataTask.java
private void getDataFoodFromJson(String JsonStr) throws JSONException { final String JSON_LIST = "foods"; final String JSON_TITLE = "title"; final String JSON_IMAGE_ID = "image_id"; final String JSON_DESCRIPTION = "description"; final String JSON_TIME = "time"; final String JSON_ID = "id"; try {/*from w ww . jav a2 s . co m*/ JSONObject dataJson = new JSONObject(JsonStr); JSONArray moviesArray = dataJson.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 title = movie.getString(JSON_TITLE); String image_id = movie.getString(JSON_IMAGE_ID); String description = movie.getString(JSON_DESCRIPTION); String time = movie.getString(JSON_TIME); String id = movie.getString(JSON_ID); ContentValues foodsValues = new ContentValues(); foodsValues.put(FoodEntry.COLUMN_ID, id); foodsValues.put(FoodEntry.COLUMN_TITLE, title); foodsValues.put(FoodEntry.COLUMN_IMAGE_ID, image_id); foodsValues.put(FoodEntry.COLUMN_DESCRIPTION, description); foodsValues.put(FoodEntry.COLUMN_TIME, time); cVVector.add(foodsValues); } int inserted = 0; // delete database int rowdeleted = mContext.getContentResolver().delete(FoodEntry.CONTENT_URI, null, null); // // add to database Log.i(LOG_TAG, "Creando registros en base de datos. Tabla Food: "); if (cVVector.size() > 0) { ContentValues[] cvArray = new ContentValues[cVVector.size()]; cVVector.toArray(cvArray); inserted = mContext.getContentResolver().bulkInsert(FoodEntry.CONTENT_URI, cvArray); Log.i(LOG_TAG, "Registros nuevos creados en tabla Food: " + inserted); } } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); } }
From source file:com.msrproduction.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 a v 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) { long locationId; 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.moveToNext()) { int locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID); locationId = locationCursor.getLong(locationIdIndex); } else { ContentValues locationValues = new ContentValues(); 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); Uri insertedUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, locationValues); locationId = ContentUris.parseId(insertedUri); } locationCursor.close(); return locationId; }
From source file:company.test.Test.java
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); switch (id) { case R.id.add_item: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Add a task"); builder.setMessage("What do you want to do?"); final EditText inputField = new EditText(this); builder.setView(inputField);//from www . j a va 2 s . com builder.setPositiveButton("Add", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String task = inputField.getText().toString(); Log.d("MainActivity", task); SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.clear(); values.put(ItemContract.Columns.ITEM, task); db.insertWithOnConflict(ItemContract.TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE); activity.updateUI(); } }); builder.setNegativeButton("Cancel", null); builder.create().show(); return true; case R.id.action_settings: Log.d("MainActivity", "Settings"); return true; default: return false; } }
From source file:com.ruuhkis.cookies.CookieSQLSource.java
/** * Updates cookie if it already exists else adds new cookie tot the table * @param cookie//from w w w . ja v a2 s . c o m */ public void addCookie(Cookie cookie) { long cookieId = -1; Cursor cursor = db.query(CookieSQLHelper.COOKIE_TABLE_NAME, null, CookieSQLHelper.COLUMN_NAME + "=?", new String[] { cookie.getName() }, null, null, null); ContentValues cookieValues = new ContentValues(); cookieValues.put(CookieSQLHelper.COLUMN_COMMENT, cookie.getComment()); cookieValues.put(CookieSQLHelper.COLUMN_COMMENT_URL, cookie.getCommentURL()); cookieValues.put(CookieSQLHelper.COLUMN_DOMAIN, cookie.getDomain()); Date date = cookie.getExpiryDate(); cookieValues.put(CookieSQLHelper.COLUMN_EXPIRY_DATE, date != null ? date.getTime() : -1); cookieValues.put(CookieSQLHelper.COLUMN_NAME, cookie.getName()); cookieValues.put(CookieSQLHelper.COLUMN_PATH, cookie.getPath()); cookieValues.put(CookieSQLHelper.COLUMN_PERSISTENT, cookie.isPersistent() ? 1 : 0); cookieValues.put(CookieSQLHelper.COLUMN_SECURE, cookie.isSecure() ? 1 : 0); cookieValues.put(CookieSQLHelper.COLUMN_VALUE, cookie.getValue()); cookieValues.put(CookieSQLHelper.COLUMN_VERSION, cookie.getVersion()); if (cursor.moveToFirst()) { cookieId = cursor.getLong(cursor.getColumnIndex(CookieSQLHelper.COLUMN_COOKIE_ID)); db.update(CookieSQLHelper.COOKIE_TABLE_NAME, cookieValues, CookieSQLHelper.COLUMN_ID + "=?", new String[] { Long.toString(cookieId) }); db.delete(CookieSQLHelper.PORT_TABLE_NAME, CookieSQLHelper.COLUMN_COOKIE_ID + "=?", new String[] { Long.toString(cookieId) }); } else { cookieId = db.insert(CookieSQLHelper.COOKIE_TABLE_NAME, null, cookieValues); } if (cookieId != -1) { int[] ports = cookie.getPorts(); if (ports != null) { for (int i = 0; i < ports.length; i++) { ContentValues portValues = new ContentValues(); portValues.put(CookieSQLHelper.COLUMN_COOKIE_ID, cookieId); portValues.put(CookieSQLHelper.COLUMN_PORT, ports[i]); db.insert(CookieSQLHelper.PORT_TABLE_NAME, null, portValues); } } } else { Log.e(TAG, "id = -1"); } cursor.close(); }
From source file:edu.htl3r.schoolplanner.backend.database.AutoSelectDatabase.java
@Override public void setAutoSelect(AutoSelectSet autoSelectSet) { final String TABLE = DatabaseAutoSelectConstants.TABLE_AUTO_SELECT_NAME; SQLiteDatabase database = this.database.openDatabase(true); this.database.deleteAllRowsWithLoginSetKey(database, TABLE); ContentValues values = new ContentValues(); values.put(DatabaseCreateConstants.TABLE_LOGINSET_KEY, this.database.getLoginSetKeyForTable()); values.put(DatabaseAutoSelectConstants.ENABLED, autoSelectSet.isEnabled()); values.put(DatabaseAutoSelectConstants.TYPE, autoSelectSet.getAutoSelectType()); values.put(DatabaseAutoSelectConstants.VALUE, autoSelectSet.getAutoSelectValue()); database.beginTransaction();//from w w w .j a v a2 s. c o m this.database.insert(database, TABLE, values); database.setTransactionSuccessful(); database.endTransaction(); this.database.closeDatabase(database); }
From source file:emroxriprap.com.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 va2 s. c om * @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; //check to see if this location already exists 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 { ContentValues locationValues = new ContentValues(); 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); Uri insertedUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, locationValues); locationId = ContentUris.parseId(insertedUri); } locationCursor.close(); return locationId; }