Home How to print out message in textfield using query and cursor
Reply: 1

How to print out message in textfield using query and cursor

Marada
1#
Marada Published in 2017-11-14 21:54:34Z

I may have formulated my question wrong but the point is I am creating a signup form, and I faced with a problem. So, imagine you are trying to register as a new user, and you typed email, username, and password. When you press "Sign up" button cursor checks whether or not your email or username in database. If it is not in database then we add it our database, but the problem is if it already exists in database then IT SHOULD PRINT MESSAGE : "username/email already exist". So, I am not able to print that message. I have added some code with "TOAST" but when I run it the app crashes. I commented the code that I added. using Toast is accepted as well as textfield

public class MainActivity extends AppCompatActivity {
DBHelper dbHelper;
EditText email, username, password, passwordconf;
Boolean checkEmail, checkUsername, checkPassword, checkPasswordconf,zbz,zbc;
SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    dbHelper = new DBHelper(this);
}
public void clickButton2(View view) throws Exception {
    email = (EditText) findViewById(R.id.email);
    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    String userName = username.getText().toString();
    String userEmail = email.getText().toString();
    if (checkEmail && checkUsername && checkPassword && checkPasswordconf) {
     if (!ValidateUser(userName, userEmail)) {
     database = dbHelper.getWritableDatabase();
     ContentValues contentValues = new ContentValues();
     contentValues.put(DBHelper.KEY_LOGIN,username.getText().toString());
     contentValues.put(DBHelper.KEY_EMAIL,email.getText().toString());
     contentValues.put(DBHelper.KEY_PASSWORD,password.getText().toString());
     database.insert(DBHelper.TABLE_NAME, null, contentValues);
Toast.makeText(this, "you have registered 
successfully!",Toast.LENGTH_SHORT).show();
     Intent intent2 = new Intent(Main2Activity.this, Main3Activity.class);
     startActivity(intent2);
        }/*else{
      database = dbHelper.getWritableDatabase();
      Cursor cursor = database.query(DBHelper.TABLE_NAME, null, null, null, 
null, null, null);
            if (cursor.getString(cursor.getColumnIndex(DBHelper.KEY_EMAIL)).equals(userEmail)){
                Toast.makeText(this, "email already exist in Database", Toast.LENGTH_SHORT).show();
            }else if (cursor.getString(cursor.getColumnIndex(DBHelper.KEY_LOGIN)).equals(userName)){
                Toast.makeText(this, "username already exist in Database", Toast.LENGTH_SHORT).show();
            }
            cursor.close();
        }*/
    }
}
public boolean ValidateUser(String userName, String userEmail) {
    database = dbHelper.getWritableDatabase();
    Cursor cursor = database.query(DBHelper.TABLE_NAME, null, 
    DBHelper.KEY_LOGIN + "=? OR " + DBHelper.KEY_EMAIL + "=?", new String[ 
{userName, userEmail}, null, null, null);
    int i = cursor.getCount();
    cursor.close();
        if(i>0){
            return true;
        }else{
            return false;
        }
}
}

DBHelper.java

public class DBHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Login_register";
public static final String TABLE_NAME = "users";
public static final String KEY_LOGIN = "login";
public static final String KEY_PASSWORD = "passsword";
public static final String KEY_EMAIL = "email";

public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + KEY_LOGIN + " TEXT," +
            KEY_PASSWORD + " TEXT," + KEY_EMAIL + " TEXT"+")");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}
}
premkamal008
2#
premkamal008 Reply to 2017-11-14 23:18:16Z

I think, you should first try running adb logcat / pidcat to find out what exactly is the crash / what caused it.

PS: Your check for the presence of values in the db is wrong. You should iterate through the cursor to find the same.

PPS: You might prefer using unboxed boolean for the values initialized.

if (null && null..)

would ideally throw NPE

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.358852 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO