简体   繁体   中英

getting wrong Toast message

public class AuthenticationActivity extends AppCompatActivity {

private EditText edtMobile,edtPassword;
private Button btnLogin;
private Button btnSignup;
DatabaseHelper databaseHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_authentication);

    databaseHelper = new DatabaseHelper(this);

    edtMobile = (EditText) findViewById(R.id.edt_mobile);
    edtPassword = (EditText) findViewById(R.id.edt_password);

    btnLogin = (Button) findViewById(R.id.btn_login);
    btnSignup = (Button) findViewById(R.id.btn_signup);

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

            String authenticationActivtyMobile = edtMobile.getText().toString();
            String authenticationActivtyPassword = edtPassword.getText().toString();

            //Mobile
            if(authenticationActivtyMobile.length() == 10){

            }else{
                Toast.makeText(AuthenticationActivity.this, "Enter Only 10 Digit Number", Toast.LENGTH_SHORT).show();
                return;
            }
            String phone = String.valueOf(authenticationActivtyMobile);
            char c  = phone.charAt(0);
            if (c == '8' || c == '9' ||c =='7'){

            }else if( c == '0' ||c == '1' ||c == '2' ||c == '3' ||c == '4' ||c == '5' ||c == '6')
            {
                Toast.makeText(AuthenticationActivity.this, "Number Must Begin with 9 8 7",Toast.LENGTH_SHORT).show();
                return;
            }
            //Password
            if(authenticationActivtyPassword.length() <4){
                Toast.makeText(AuthenticationActivity.this, "Password Must Have Minimum 4 Character", Toast.LENGTH_SHORT).show();
                return;
            }else if(authenticationActivtyPassword.length()>=15){
                Toast.makeText(AuthenticationActivity.this, "Password Can Have Maximum 8 Character", Toast.LENGTH_SHORT).show();
                return;
            }


           String  password = databaseHelper.search(authenticationActivtyMobile,authenticationActivtyPassword);
            if (authenticationActivtyPassword.equals(password) && authenticationActivtyMobile.equals(password) ) {
                Toast.makeText(AuthenticationActivity.this, "LOGIN SUCCESS", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(AuthenticationActivity.this, "LOGIN FAILED", Toast.LENGTH_SHORT).show();
            }

        }
    });
    btnSignup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(AuthenticationActivity.this, "Opening MainActivity Page", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(AuthenticationActivity.this, MainActivity.class);
            startActivity(intent);
        }
    });
}

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

public static String dataBaseName = "Login.db";

private static final int dataBaseVersion = 1;

private static final String tableName = "Accounts";
private static String Key_Id = "id";
private static String Key_FirstName = "firstname";
private static String Key_LastName = "lastname";
private static String Key_Password = "password";
private static String Key_Mobile = "mobile";
private static String Key_Email = "email";

public static String tag = "tag";

private static final String createTableAccounts = "CREATE TABLE " + tableName + "( " + Key_Id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Key_FirstName + " TEXT, " + Key_LastName + " TEXT, " + Key_Password + " TEXT, " + Key_Mobile + " TEXT, " + Key_Email + " TEXT );";

public DatabaseHelper(Context context) {
    super(context, dataBaseName, null, dataBaseVersion);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(createTableAccounts);

}

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

    db.execSQL("DROP TABLE IF EXISTS" + createTableAccounts);
    onCreate(db);

}

public long addAccountDetials(AccountsModel accounts) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(Key_FirstName, accounts.firstname);
    values.put(Key_LastName, accounts.lastname);
    values.put(Key_Password, accounts.password);
    values.put(Key_Mobile, accounts.mobile);
    values.put(Key_Email, accounts.email);

    long insert = db.insert(tableName, null, values);
    return insert;
}

public int updateEntry(AccountsModel accounts) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(Key_FirstName, accounts.firstname);
    values.put(Key_LastName, accounts.lastname);
    values.put(Key_Password, accounts.password);
    values.put(Key_Mobile, accounts.mobile);
    values.put(Key_Email, accounts.email);

    return db.update(tableName, values, Key_Id + "=?", new String[]{String.valueOf(accounts.id)});
}

public void deleteEntry(long id) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(tableName, Key_Id + " = ?", new String[]{String.valueOf(id)});
}


public String search(String mobile,String password) {

    SQLiteDatabase db = this.getReadableDatabase();
    String query = "Select * FROM Accounts WHERE mobile='"+mobile+"'and password='"+password+"'";
    Cursor cursor = db.rawQuery(query, null);
    String a,b;
    b = "not found";
    if (cursor.moveToFirst()) {
        do {
            a = cursor.getString(3);
            if (a.equals(mobile)) {
                b = cursor.getString(4);
                break;
            }
        }
        while (cursor.moveToFirst());
    }
    return b;
}}

MainActivity.java

public class MainActivity extends Activity implements OnClickListener{

private String firstName;
private String lastName;
private String mobile;
private String password;
private String email;

private EditText edtSignupFirstName;
private EditText edtSignupLastName;
private EditText edtSignupMobile;
private EditText edtSignupPassword;
private EditText edtSignupEmail;
private EditText edtId;

private Button btnSignupRegister;
private Button btnDelete;

DatabaseHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = new DatabaseHelper(getApplicationContext());

    edtSignupFirstName=(EditText)findViewById(R.id.edt_signup_first_name);
    edtSignupLastName=(EditText)findViewById(R.id.edt_signup_last_name);
    edtSignupMobile=(EditText)findViewById(R.id.edt_signup_mobile);
    edtSignupPassword=(EditText)findViewById(R.id.edt_signup_password);
    edtSignupEmail=(EditText)findViewById(R.id.edt_signup_email);
    edtId=(EditText)findViewById(R.id.edt_id);

    btnSignupRegister=(Button)findViewById(R.id.btn_signup_register);
    btnDelete=(Button)findViewById(R.id.btn_delete);

    btnSignupRegister.setOnClickListener(this);
    btnDelete.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    if (v==findViewById(R.id.btn_signup_register))
    {
        AccountsModel accounts=new AccountsModel();
        accounts.firstname=edtSignupFirstName.getText().toString();
        accounts.lastname=edtSignupLastName.getText().toString();
        accounts.password=edtSignupPassword.getText().toString();
        accounts.mobile=edtSignupMobile.getText().toString();
        accounts.email=edtSignupEmail.getText().toString();
        db.addAccountDetials(accounts);

        Toast.makeText(MainActivity.this, "DB ADDED", Toast.LENGTH_SHORT).show();

    }
    if (v==findViewById(R.id.btn_delete))
    {
        String account_id=edtId.getText().toString();
        db.deleteEntry(Integer.parseInt(account_id));

        Toast.makeText(MainActivity.this, "DB DELETED", Toast.LENGTH_SHORT).show();
    }
}
}

Here by clicking signup button it intent to main activity where my details will be entered. Then by clicking the register button, details will be stored in db.

Here when I click Login button it matches to db and getting toast message "Login Failed". What should I do to get the right toast message?

it matches to db and getting toast message "Login Failed"

Because of :

if (authenticationActivtyPassword.equals(password) &&
                      authenticationActivtyMobile.equals(password) ) {
 ///..
}

if condition

Currently checking authenticationActivtyPassword and authenticationActivtyMobile both is equals to password , which make if condition always false.

and in search method, fetching data from db using both mobile and password , so you can change if condition as:

if (authenticationActivtyPassword.equals(password)) {
 ///.. login success
}else{
  //login failed
}
authenticationActivtyMobile.equals(password)  --that is never going to be true.

Instead you can return a boolean from your search function and just check if it is true like:

public boolean search(String mobile,String password) {

boolean isLogin = false;
    SQLiteDatabase db = this.getReadableDatabase();
    String query = "Select * FROM Accounts WHERE mobile='"+mobile+"'and password='"+password+"'";
    Cursor cursor = db.rawQuery(query, null);
    String a,b;
    b = "not found";
    if (cursor.moveToFirst()) {
        do {
            a = cursor.getString(3);
            if (a.equals(mobile)) {
                b = cursor.getString(4);
                break;
            }
        }
        while (cursor.moveToFirst());
    }
    if(!b.equals("not found")) isLogin = true;
    return isLogin;
}}  

and then:

 boolean  isLogin = databaseHelper.search(authenticationActivtyMobile,authenticationActivtyPassword);
            if (isLogin) {
                Toast.makeText(AuthenticationActivity.this, "LOGIN SUCCESS", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(AuthenticationActivity.this, "LOGIN FAILED", Toast.LENGTH_SHORT).show();
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM