List of usage examples for android.content ContentValues ContentValues
public ContentValues()
From source file:com.example.home.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 va 2 s .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) { // 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.jll.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 w ww . java2 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 String selection = WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + "= ?" + " AND " + WeatherContract.LocationEntry.COLUMN_CITY_NAME + "= ?" + " AND " + WeatherContract.LocationEntry.COLUMN_COORD_LAT + "= ?" + " AND " + WeatherContract.LocationEntry.COLUMN_COORD_LONG + "= ?"; String[] selectionArgs = new String[] { locationSetting, cityName, String.valueOf(lat), String.valueOf(lon) }; Cursor cursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI, new String[] { WeatherContract.LocationEntry._ID }, selection, selectionArgs, null); long id = -1; if (cursor.moveToFirst()) { id = cursor.getLong(0); } else { 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 newRow = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, values); id = ContentUris.parseId(newRow); } // If it exists, return the current ID // Otherwise, insert it using the content resolver and the base URI return id; }
From source file:com.sidviv.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 ww .ja va 2s .c om public 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.arminkale.android.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 ww w. j a v a2 s.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) { // 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(LocationEntry.CONTENT_URI, new String[] { LocationEntry._ID }, 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(LocationEntry.COLUMN_CITY_NAME, cityName); locationValues.put(LocationEntry.COLUMN_LOCATION_SETTING, locationSetting); locationValues.put(LocationEntry.COLUMN_COORD_LAT, lat); locationValues.put(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); } // Close the cursor and return the new ID. locationCursor.close(); return locationId; }
From source file:za.co.neilson.alarm.database.Database.java
public static long create(Alarm alarm) { ContentValues cv = new ContentValues(); cv.put(COLUMN_ALARM_ACTIVE, alarm.getAlarmActive()); cv.put(COLUMN_ALARM_TIME, alarm.getAlarmTimeString()); try {/* w w w . ja va 2s . c om*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = null; oos = new ObjectOutputStream(bos); oos.writeObject(alarm.getDays()); byte[] buff = bos.toByteArray(); cv.put(COLUMN_ALARM_DAYS, buff); } catch (Exception e) { } Log.v("Alarm Active : ", "" + alarm.getAlarmActive()); Log.v("Alarm Name : ", "" + alarm.getAlarmName()); cv.put(COLUMN_ALARM_DIFFICULTY, alarm.getDifficulty().ordinal()); cv.put(COLUMN_ALARM_TONE, alarm.getAlarmTonePath()); cv.put(COLUMN_ALARM_VIBRATE, alarm.getVibrate()); cv.put(COLUMN_ALARM_NAME, alarm.getAlarmName()); return getDatabase().insert(ALARM_TABLE, null, cv); }
From source file:com.jcjacksonengineering.android.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 .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; // 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:edu.stanford.mobisocial.dungbeetle.Helpers.java
public static Uri insertGroup(final Context c, String groupName, String dynUpdateUri, String feedName) { assert (groupName != null && dynUpdateUri != null && feedName != null); ContentValues values = new ContentValues(); values.put(Group.NAME, groupName); values.put(Group.DYN_UPDATE_URI, dynUpdateUri); values.put(Group.FEED_NAME, feedName); return c.getContentResolver().insert(Uri.parse(DungBeetleContentProvider.CONTENT_URI + "/groups"), values); }
From source file:de.lebenshilfe_muenster.uk_gebaerden_muensterland.database.SignDAO.java
/** * Persist a sign. For <strong>testing</strong> purposes only. * * @param sign a Sign, which has not been persisted yet. * @return the persisted sign, <code>null</code> if persisting failed. *//* w ww. j av a 2 s. co m*/ public Sign create(Sign sign) { Log.d(TAG, "Creating sign: " + sign); this.database.beginTransaction(); Sign createdSign = null; try { final ContentValues values = new ContentValues(); values.put(DbContract.SignTable.COLUMN_NAME_SIGN_NAME, sign.getName()); values.put(DbContract.SignTable.COLUMN_NAME_SIGN_NAME_DE, sign.getNameLocaleDe()); values.put(DbContract.SignTable.COLUMN_NAME_MNEMONIC, sign.getMnemonic()); if (sign.isStarred()) { values.put(DbContract.SignTable.COLUMN_NAME_STARRED, 1); } else { values.put(DbContract.SignTable.COLUMN_NAME_STARRED, 0); } values.put(DbContract.SignTable.COLUMN_NAME_LEARNING_PROGRESS, sign.getLearningProgress()); final long insertId = this.database.insert(DbContract.SignTable.TABLE_NAME, null, values); if (-1 == insertId) { throw new IllegalStateException( MessageFormat.format("Inserting sign: {0} failed due to" + " a database error!", sign)); } createdSign = readSingleSign(insertId); this.database.setTransactionSuccessful(); Log.d(TAG, "Created sign: " + createdSign); } finally { this.database.endTransaction(); } return createdSign; }
From source file:com.codebutler.farebot.activities.AddKeyActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_key); getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.FILL_PARENT); findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() { @Override//from w ww .java 2s .c o m public void onClick(View view) { setResult(RESULT_CANCELED); finish(); } }); findViewById(R.id.add).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final String keyType = ((RadioButton) findViewById(R.id.is_key_a)).isChecked() ? ClassicSectorKey.TYPE_KEYA : ClassicSectorKey.TYPE_KEYB; new BetterAsyncTask<Void>(AddKeyActivity.this, true, false) { @Override protected Void doInBackground() throws Exception { ClassicCardKeys keys = ClassicCardKeys.fromDump(keyType, mKeyData); ContentValues values = new ContentValues(); values.put(KeysTableColumns.CARD_ID, mTagId); values.put(KeysTableColumns.CARD_TYPE, mCardType); values.put(KeysTableColumns.KEY_DATA, keys.toJSON().toString()); getContentResolver().insert(CardKeyProvider.CONTENT_URI, values); return null; } @Override protected void onResult(Void unused) { Intent intent = new Intent(AddKeyActivity.this, KeysActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); } }.execute(); } }); mNfcAdapter = NfcAdapter.getDefaultAdapter(this); Utils.checkNfcEnabled(this, mNfcAdapter); Intent intent = getIntent(); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); mPendingIntent = PendingIntent.getActivity(this, 0, intent, 0); if (getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_VIEW) && getIntent().getData() != null) { try { InputStream stream = getContentResolver().openInputStream(getIntent().getData()); mKeyData = IOUtils.toByteArray(stream); } catch (IOException e) { Utils.showErrorAndFinish(this, e); } } else { finish(); } }
From source file:com.example.shutapp.DatabaseHandler.java
/** * Adds a chatroom to the database.//from w w w. ja v a 2s . co m * @param chatroom */ public void addChatroom(Chatroom chatroom) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, chatroom.getName()); // Chatroom Name values.put(KEY_LATITUDE, chatroom.getLatitude()); // Chatroom Latitude values.put(KEY_LONGITUDE, chatroom.getLongitude()); // Chatroom Longitude values.put(KEY_RADIUS, chatroom.getRadius()); // Chatroom Radius // Inserting Row db.insert(TABLE_CHATROOMS, null, values); db.close(); // Closing database connection }