Android - 为输入字段添加验证

时间:2017-06-19 21:07:05

标签: android sqlite validation android-contentprovider

我制作的Android应用使用sqlite database存储应用数据,activity类用于编辑/插入数据,DbHelper用于管理数据库创建和版本,以及管理数据库访问权限的content provider

我设法从数据库中获取要查询,插入,删除和编辑的数据。但是,我不太清楚如何向用户添加视觉反馈。我已尝试在Toast添加IllegalArgumentException,但应用程序只会将内容添加到数据库中。

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

这是insert

content provider方法的摘要
@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);
}

这是活动文件

的摘录
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的{​​{1}}中的菜单:

savePatient

1 个答案:

答案 0 :(得分:1)

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

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