【发布时间】:2017-06-19 21:07:05
【问题描述】:
我正在制作一个 Android 应用程序,它使用 sqlite database 来存储应用程序数据,activity 类来编辑/插入数据,DbHelper 来管理数据库的创建和版本,以及 content provider管理对数据库的访问。
我已经设法从数据库中获取要查询、插入、删除和编辑的数据。但是,我不太确定如何向用户添加视觉反馈。我尝试在我放置 IllegalArgumentException 的位置添加 Toast,但应用程序只会将内容添加到数据库中。
如果我省略名称,应用程序将触发定义的IllegalArgumentException,然后使应用程序崩溃。
这是content provider 中insert 方法的sn-p
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
final int match = sUriMatcher.match(uri);
switch (match) {
case PATIENT:
return insertPatient(uri, contentValues);
default:
throw new IllegalArgumentException("Insertion is not supported for " + uri);
}
}
/**
* Insert a patient into the database with the given content values.
*/
private Uri insertPatient(Uri uri, ContentValues values) {
String name = values.getAsString(PatientEntry.COLUMN_PATIENT_NAME);
if (name == null || name.length()==0) {
//Toast.makeText(getContext(), "Patient requires a name", Toast.LENGTH_SHORT).show();
throw new IllegalArgumentException("Patient requires a name");
}
Integer weight = values.getAsInteger(PatientEntry.COLUMN_PATIENT_WEIGHT);
if (weight != null && weight < 0) {
throw new IllegalArgumentException("Patient requires valid weight");
}
SQLiteDatabase database = mDbHelper.getWritableDatabase();
long id = database.insert(PatientEntry.TABLE_NAME, null, values);
if (id == -1) {
Log.e(LOG_TAG, "Failed to insert row for " + uri);
return null;
}
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(uri, id);
}
这是来自活动文件的 sn-p
private void savePatient() {
String nameString = mNameEditText.getText().toString().trim();
String weightString = mWeightEditText.getText().toString().trim();
if (mCurrentPatientUri == null &&
TextUtils.isEmpty(nameString) && TextUtils.isEmpty(weightString) {
Toast.makeText(this, "Data was not saved", Toast.LENGTH_SHORT).show();
return;
}
ContentValues values = new ContentValues();
values.put(PatientEntry.COLUMN_PATIENT_NAME, nameString);
int weight = 0;
if (!TextUtils.isEmpty(weightString)) {
weight = Integer.parseInt(weightString);
}
values.put(PatientEntry.COLUMN_PATIENT_WEIGHT, weight);
// Determine if this is a new or existing Patient by checking if mCurrentPatientUri is null or not
if (mCurrentPatientUri == null) {
// This is a NEW patient
Uri newUri = getContentResolver().insert(PatientEntry.CONTENT_URI, values);
if (newUri == null) {
Toast.makeText(this, getString(R.string.editor_insert_patient_failed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, getString(R.string.editor_insert_patient_successful),
Toast.LENGTH_SHORT).show();
}
} else {
// Otherwise this is an EXISTING patient
int rowsAffected = getContentResolver().update(mCurrentPatientUri, values, null, null);
if (rowsAffected == 0) {
Toast.makeText(this, getString(R.string.editor_update_patient_failed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, getString(R.string.editor_update_patient_successful),
Toast.LENGTH_SHORT).show();
}
}
}
有人可以帮忙吗?
.................................................. .....
编辑 1:
这是activity中调用savePatient的菜单:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu
switch (item.getItemId()) {
case R.id.action_save:
savePatient();
finish();
return true;
case R.id.action_delete:
showDeleteConfirmationDialog();
return true;
case android.R.id.home:
if (!mPatientHasChanged) {
NavUtils.navigateUpFromSameTask(EditorActivity.this);
return true;
}
// Otherwise if there are unsaved changes, setup a dialog to warn the user.
DialogInterface.OnClickListener discardButtonClickListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
NavUtils.navigateUpFromSameTask(EditorActivity.this);
}
};
showUnsavedChangesDialog(discardButtonClickListener);
return true;
}
return super.onOptionsItemSelected(item);
}
【问题讨论】:
-
无论哪个层发出请求,都需要捕获异常并显示合适的 UI。这层代码甚至不应该知道你的应用有一个 UI。
标签: android sqlite validation android-contentprovider