如何验证注册页面android

时间:2016-07-25 19:44:30

标签: android validation android-fragments

编辑:例如,如果我这样做:

            boolean thiscreateserror = false;
            if (thiscreateserror == true) {
                mListener.register(userName, password);
            }
            else{}

07-25 20:24:25.552 2385-2529/com.example.hoofdgebruiker.winkelskortrijk W/System: ClassLoader referenced unknown path: /data/data/com.example.hoofdgebruiker.winkelskortrijk/lib
07-25 20:24:25.574 2385-2555/com.example.hoofdgebruiker.winkelskortrijk E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4017ef0
07-25 20:24:27.559 2385-2555/com.example.hoofdgebruiker.winkelskortrijk E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa1e6d10
编辑2:我很抱歉。每次我尝试在注册后重新启动应用程序时,我似乎都会收到此错误。也许问题出在其他地方?

我的数据库:

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "winkelskortrijk.db";
private static final String TABLE_USERS = "users";
private static final String TABLE_FAVOURITES = "favorieten";

public static final String COLUMN_ID = "id";
public static final String COLUMN_USERNAME = "username";
public static final String COLUMN_PASSWORD = "password";

public static final String COLUMN_ID2 = "id2";
public static final String COLUMN_NAME = "naam";
public static final String COLUMN_STRAAT = "straat";
public static final String COLUMN_HUISNR = "nr";
public static final String COLUMN_POSTCODE = "postcode";
public static final String COLUMN_DEELGEMEENTE = "deelgemeente";
public static final String COLUMN_GEMEENTE = "gemeente";
public static final String COLUMN_ADRES = "adres";

public MyDBHandler(Context context, String name,
                   SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_USERS_TABLE = "CREATE TABLE " +
            TABLE_USERS + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_USERNAME + " TEXT,"
            + COLUMN_PASSWORD + " TEXT"
            + ")";
    db.execSQL(CREATE_USERS_TABLE);

    String CREATE_FAVOURITES_TABLE = "CREATE TABLE " +
            TABLE_FAVOURITES + "("
            + COLUMN_ID2 + " INTEGER PRIMARY KEY,"
            + COLUMN_NAME + " TEXT,"
            + COLUMN_STRAAT + " TEXT,"
            + COLUMN_HUISNR + " INTEGER,"
            + COLUMN_POSTCODE + " INTEGER,"
            + COLUMN_DEELGEMEENTE + " TEXT,"
            + COLUMN_GEMEENTE + " TEXT,"
            + COLUMN_ADRES + " TEXT,"
            + COLUMN_ID + " INT, "
            + "FOREIGN KEY(" + COLUMN_ID + ") REFERENCES "
            + TABLE_USERS + "(id) " + ")";
    db.execSQL(CREATE_FAVOURITES_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
                      int newVersion) {

}


public User findUser(User user) {

    String selectQuery = "SELECT  * FROM " + TABLE_USERS + " WHERE "
            + COLUMN_USERNAME + " = '" + user.getUserName()+"'";

    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor = db.rawQuery(selectQuery, null);

    User findUser = new User();

    if (cursor.moveToFirst()) {
        cursor.moveToFirst();
        findUser.setUserName(cursor.getString(1));
        findUser.setPassword(cursor.getString(2));
        cursor.close();
    } else {
        findUser = null;
    }
    db.close();
    return findUser;

}

public void addOrUpdateUser(User user){

    ContentValues values = new ContentValues();
    values.put(COLUMN_USERNAME, user.getUserName());
    values.put(COLUMN_PASSWORD, user.getPassword());
    SQLiteDatabase db = this.getWritableDatabase();

    db.insert(TABLE_USERS, null, values);
    db.close();
}

我的主要活动:

@Override
public void register(String userName, String password){
    User userNew = new User();
    User user = new User();
    userNew.setUserName(userName);
    userNew.setPassword(password);
    MyDBHandler dbHandler = new MyDBHandler(this,null,null,1);
    user = dbHandler.findUser(userNew);
    if (user == null){
        dbHandler.addOrUpdateUser(userNew);
        navigateToLogin();
    }
    else{
        navigateToRegister();
    }
}

}

2 个答案:

答案 0 :(得分:0)

当你写if(password != passCon)时 您比较引用而不是字符串值。 请尝试使用if(!passCon.equals(password))

答案 1 :(得分:0)

将您的代码更改为以下内容:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_register, container, false);
    Button btnRegister = (Button) view.findViewById(R.id.actionregister);
    final EditText nameText = (EditText) view.findViewById(R.id.rusername);
    final EditText passText = (EditText) view.findViewById(R.id.rpassword);
    final EditText passConText = (EditText) view.findViewById(R.id.rpasswordr);

    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(validate(nameText, passText, passConText))
                mListener.register(userName, password);
        }

    });
    return view;
}

private boolean validate(EditText nameText, EditText passText, EditText passConText) {


        String userName = nameText.getText().toString();
        String password = passText.getText().toString();
        String passCon = passConText.getText().toString();
     boolean isValidated = true;
    if(userName.trim().length() == 0){
        nameText.setError("Username has to be entered");
        isValidated = false;
    }
    if(password.trim().length() == 0)
    {
        passText.setError("Password has to be entered");
        isValidated = false;
    }
    else {
    if(!pass.equals(passCon)){
            passText.setError("Passwords have to be matching");
            isValidated = false;
    }
return isValidated;

}
  

注意:创建nameText,passText实例变量,以便可以访问它   在函数内部。