/**
* Updates the data at the given selection and selection arguments, with the new ContentValues.
*/
@Override
public int update(Uri uri, ContentValues contentValues, String selection,
String[] selectionArgs) {
final int match = sUriMatcher.match(uri);
switch (match) {
case TASKS:
return updatetask(uri, contentValues, selection, selectionArgs);
case TASKS_ID:
// For the task_ID code, extract out the ID from the URI,
// so we know which row to update. Selection will be "_id=?" and selection
// arguments will be a String array containing the actual ID.
selection = TaskContract.TaskEntry._ID + "=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
return updatetask(uri, contentValues, selection, selectionArgs);
default:
throw new IllegalArgumentException("Update is not supported for " + uri);
}
}
/**
* Update tasks in the database with the given content values. Apply the changes to the rows
* specified in the selection and selection arguments (which could be 0 or 1 or more tasks).
* Return the number of rows that were successfully updated.
*/
private int updatetask(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
// If the {@link TaskEntry#COLUMN_task_NAME} key is present,
// check that the name value is not null.
if (values.containsKey(TaskContract.TaskEntry.COLUMN_TASK_NAME)) {
String name = values.getAsString(TaskContract.TaskEntry.COLUMN_TASK_NAME);
if (name == null) {
throw new IllegalArgumentException("task requires a name");
}
}
// If the {@link TaskEntry#COLUMN_task_GENDER} key is present,
// check that the gender value is valid.
if (values.containsKey(TaskContract.TaskEntry.COLUMN_TASK_DATE)) {
String date = values.getAsString(TaskContract.TaskEntry.COLUMN_TASK_DATE);
if (date == null) {
throw new IllegalArgumentException("Enter valid date.");
}
}
// If the {@link TaskEntry#COLUMN_task_WEIGHT} key is present,
// check that the weight value is valid.
if (values.containsKey(TaskContract.TaskEntry.COLUMN_TASK_TIME)) {
// Check that the weight is greater than or equal to 0 kg
String time = values.getAsString(TaskContract.TaskEntry.COLUMN_TASK_TIME);
if (time == null) {
throw new IllegalArgumentException("task requires valid time ");
}
}
// If there are no values to update, then don't try to update the database
if (values.size() == 0) {
return 0;
}
// Otherwise, get writeable database to update the data
SQLiteDatabase database = mDbHelper.getWritableDatabase();
// Perform the update on the database and get the number of rows affected
int rowsUpdated = database.update(TaskContract.TaskEntry.TABLE_NAME, values, selection, selectionArgs);
if (rowsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsUpdated;
}
以上是我的内容提供商更新方法的代码。
/**
* Get user input from editor and save new task into database.
*/
private void saveTask() {
// Read from input fields
// Use trim to eliminate leading or trailing white space
String nameString = mNameEditText.getText().toString().trim();
String dateString = mDateEditText.getText().toString().trim();
String timeString = mTimeEditText.getText().toString().trim();
String locationString = mLocationEditText.getText().toString().trim();
if (mNotifySwitch.isChecked()){
mToNotify = TaskContract.TaskEntry.NOTIFY_YES;
}
if (mCurrenttaskUri == null &&
TextUtils.isEmpty(nameString) && TextUtils.isEmpty(dateString) &&
TextUtils.isEmpty(timeString) && TextUtils.isEmpty(locationString)) {
return;
}
// Create a ContentValues object where column names are the keys,
// and task attributes from the editor are the values.
ContentValues values = new ContentValues();
values.put(TaskContract.TaskEntry.COLUMN_TASK_NAME, nameString);
values.put(TaskContract.TaskEntry.COLUMN_TASK_DATE, dateString);
values.put(TaskContract.TaskEntry.COLUMN_TASK_TIME, timeString);
String location = null;
if (!TextUtils.isEmpty(locationString)) {
location = locationString;
}
values.put(TaskContract.TaskEntry.COLUMN_TASK_LOCATION, location);
values.put(TaskContract.TaskEntry.NOTIFY_LOCATION, mToNotify);
// Insert a new row for task in the database, returning the ID of that new row.
// long newRowId = db.insert(TaskContract.TaskEntry.TABLE_NAME, null, values);
if (mCurrenttaskUri == null) {
Uri newUri = getContentResolver().insert(TaskContract.TaskEntry.CONTENT_URI, values);
if (newUri == null) {
// If the new content URI is null, then there was an error with insertion.
Toast.makeText(this, "Inserting task failed!",
Toast.LENGTH_SHORT).show();
} else {
// Otherwise, the insertion was successful and we can display a toast.
Toast.makeText(this, "Inserting task successful!",
Toast.LENGTH_SHORT).show();
}
} else {
int rowsAffected = getContentResolver().update(TaskContract.TaskEntry.CONTENT_URI, values, null, null);
if (rowsAffected == 0) {
Toast.makeText(this, "Updating task failed!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Updating task successful!.",
Toast.LENGTH_SHORT).show();
}
}
}
现在,上面是提供内容值的代码。 查询和插入工作正常,但是,当我在应用程序中更新一行时,所有行都会填充相同的更新数据。 请帮忙.. 下面是表创建的代码。
@Override
public void onCreate(SQLiteDatabase db) {
// Create a String that contains the SQL statement to create the tasks table
String SQL_CREATE_TASKS_TABLE = "CREATE TABLE " + TaskContract.TaskEntry.TABLE_NAME + " ("
+ TaskContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ TaskContract.TaskEntry.COLUMN_TASK_NAME + " TEXT NOT NULL, "
+ TaskContract.TaskEntry.COLUMN_TASK_DATE + " TEXT NOT NULL, "
+ TaskContract.TaskEntry.COLUMN_TASK_TIME + " TEXT NOT NULL, "
+ TaskContract.TaskEntry.COLUMN_TASK_LOCATION + " TEXT, "
+ TaskContract.TaskEntry.NOTIFY_LOCATION + " INTEGER NOT NULL);";
// Execute the SQL statement
db.execSQL(SQL_CREATE_TASKS_TABLE);
}
需要立即帮助,请回复