2015-11-07 91 views
0
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(); 
    } 
} 
} 

这里点击注册按钮,它的意图到我的个人资料将被输入的主要活动。然后通过点击注册按钮,细节将被存储在分贝。

在这里,当我点击登录按钮它匹配数据库和获取吐司消息“登录失败”。我应该怎么做才能获得正确的吐司信息?

+0

您的代码段太长,请编辑并减少到MCVE http://stackoverflow.com/help/mcve – kebs

回答

0

它匹配到数据库,并让烤面包的消息 “登录失败”

因为:

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

if条件

目前检查authenticationActivtyPasswordauthenticationActivtyMobile两个是等于password如果条件总是假的

search方法,从数据库中同时使用mobilepassword,这样你就可以改变,如果条件为获取数据:

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

它显示登录通过尝试这种代码的失败太 – Rahul

+0

@Rahul:使用日志和检查什么妳从db获取'password' –

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

Toast只有“登录失败” – Rahul

+0

请检查/验证代码的其余部分,数据库部分,搜索查询,它返回的内容,尝试调试 – user2450263