错误的Toast消息

时间:2015-11-07 06:59:06

标签: java android sqlite

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

这里通过点击注册按钮,它意图进入主要活动,我的详细信息将被输入。然后单击注册按钮,详细信息将存储在db。

在这里,当我点击“登录”按钮时,它与数据库匹配并获取Toast消息&#34;登录失败&#34;。我该怎么做才能获得正确的祝酒消息?

2 个答案:

答案 0 :(得分:0)

  

它匹配db并获取toast消息“Login Failed”

因为:

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

if条件

目前检查authenticationActivtyPasswordauthenticationActivtyMobile两者都等于password,这会使条件始终为假。

并在search方法中,使用mobilepassword从数据库中获取数据,因此您可以将条件更改为:

if (authenticationActivtyPassword.equals(password)) {
 ///.. login success
}else{
  //login failed
}

答案 1 :(得分:0)

authenticationActivtyMobile.equals(password)  --that is never going to be true.

相反,您可以从搜索功能返回一个布尔值,只需检查它是否为真:

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

然后:

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