user3227070 user3227070 - 4 months ago 8
Android Question

How to validate register page android

Edit: for instance, if I just do this:

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


Edit2: I'm very sorry. I actually seem to be getting this error every time I try to relaunch the app after registering. Maybe the problem lies somewhere else?

My database:

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();
}


My main activity:

@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();
}
}

}

Answer

Change your code to something like this:

@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;

}

Note : Make nameText, passText instance variable so it is accessible inside the function.

Comments