Android-向输入字段添加验证

Android-向输入字段添加验证,android,sqlite,validation,android-contentprovider,Android,Sqlite,Validation,Android Contentprovider,我正在制作一个Android应用程序,它使用sqlite数据库存储应用程序数据,使用活动类编辑/插入数据,使用DbHelper管理数据库创建和版本,使用内容提供者管理对数据库的访问 我已设法从数据库中获取要查询、插入、删除和编辑的数据。但是,我不太确定如何向用户添加视觉反馈。我试着在我放置了一个IllegalArgumentException的地方添加一个Toast,但应用程序只会将内容添加到数据库中 如果省略名称,应用程序将触发已定义的IllegalArgumentException,然后使应

我正在制作一个Android应用程序,它使用
sqlite数据库
存储应用程序数据,使用
活动
类编辑/插入数据,使用
DbHelper
管理数据库创建和版本,使用
内容提供者
管理对数据库的访问

我已设法从数据库中获取要查询、插入、删除和编辑的数据。但是,我不太确定如何向用户添加视觉反馈。我试着在我放置了一个
IllegalArgumentException
的地方添加一个
Toast
,但应用程序只会将内容添加到数据库中

如果省略名称,应用程序将触发已定义的
IllegalArgumentException
,然后使应用程序崩溃

这是
内容提供程序

@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);
}
有人能帮忙吗

编辑1: 这是调用
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);
}

对于视觉反馈,应尽早进行验证。 例如,在对某些用户操作调用save patient之前,只需调用validatePatient(),如果失败,则不会调用save


对于视觉反馈,您可以在字段下方显示错误文本,这些文本只有在与该字段相关的验证失败时才会显示。

发出请求的任何层都需要捕获异常并显示适当的UI。这一层代码甚至不应该知道您的应用程序有一个UI。我已经编辑了我的问题,将选项ItemSelected包含在内,所以我会在开关块中的savePatient()之前调用validation?是的。例如validatePatient(){if(newUri==null){patientUriErrorTextView=(TextView)findViewById(R.id.patientUriErrorTextView);PatientUrrorTextView.setVisibility(View.VISIBLE);return false;}return true;}在您选择的选项中,您可以执行类似于if(validatePatient()){savePatient();finish()的操作返回true;此外,我还建议不要夸大您的活动职责,而是将这些任务委托给ValidationHandler之类的类,保存任务可以委托给PatientPositionManager,或者根据您的上下文委托给其他类。:)
@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);
}