我正在尝试编写用于登录的android代码并使用SQL进行注册,当我运行该应用并尝试使用与我注册的相同用户名登录时,它不会转到首页,但是会执行else语句在登录类中显示“输入错误”。 请问有人知道原因吗?
这是我的代码:
public class Login extends AppCompatActivity {
EditText edt_username, edt_pass,edt_email;
TextView tv_username, tv_pass;
Button btn_login, btn_signUp;
Login_SQLHelper login_sqlHelper;
SQLiteDatabase db;
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_sqlHelper = new Login_SQLHelper(this);
edt_username = (EditText) findViewById(R.id.edt_username);
edt_pass = (EditText) findViewById(R.id.edt_pass);
tv_username = (TextView) findViewById(R.id.tv_username);
tv_pass = (TextView) findViewById(R.id.tv_pass);
btn_login = (Button) findViewById(R.id.button);
btn_signUp = (Button) findViewById(R.id.button2);
btn_signUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent goSignup = new Intent(Login.this, SignUp.class);
startActivity(goSignup);
}
});
login();
//viewData();
}
public void login() {
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
user = new User();
String name = edt_username.getText().toString();
String pass = edt_pass.getText().toString();
user.setUser_name(name);
user.setPassword(pass);
if ( name.equals(login_sqlHelper.get_data().getUser_name())){
Intent goHome = new Intent(Login.this, Home.class);
startActivity(goHome);
} else
Toast.makeText(Login.this, "Wrong input", Toast.LENGTH_SHORT).show();
edt_username.setText("");
edt_pass.setText("");
public class Login_SQLHelper extends SQLiteOpenHelper {
public static final String database_name = "user_log";
public static final String table_name = "user_table";
public static final String col_username = "username";
public static final String col_userEmail = "user_email";
public static final String col_userPass = "user_pass";
User user = new User();
public Login_SQLHelper(Context context) {
super(context, database_name, null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(" create table " + table_name + "(" + col_username + " TEXT," + col_userEmail + " TEXT," + col_userPass + " TEXT )");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + table_name);
}
public boolean insert_data(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col_username, user.getUser_name());
contentValues.put(col_userEmail, user.getEmail());
contentValues.put(col_userPass, user.getPassword());
Long res = db.insert(table_name, null, contentValues);
if (res == -1)
return false;
else
return true;
}
public User get_data() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from " + table_name, null);
User user2 = new User();
return user2;
}
public class SignUp extends AppCompatActivity {
EditText edt_username, edt_pass, edt_email;
Button btn3;
User user;
Login_SQLHelper login_db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
edt_username = (EditText) findViewById(R.id.edtS_username);
edt_pass = (EditText) findViewById(R.id.edtS_pass);
edt_email = (EditText) findViewById(R.id.edtS_email);
btn3 = (Button) findViewById(R.id.button3);
user = new User();
login_db = new Login_SQLHelper(this);
signUp();
}
public void signUp() {
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
user.setUser_name(edt_username.getText().toString());
user.setEmail(edt_email.getText().toString());
user.setPassword(edt_pass.getText().toString());
boolean isInserted = login_db.insert_data(user);
if (isInserted == true) {
Toast.makeText(SignUp.this, "Inserted", Toast.LENGTH_SHORT).show();
Intent goLogin = new Intent(SignUp.this, Login.class);
startActivity(goLogin);
}else
Toast.makeText(SignUp.this, "Not inserted", Toast.LENGTH_SHORT).show();
}
});
}
}
public class User {
String user_name , email , password;
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
答案 0 :(得分:0)
此方法存在问题。
public User get_data() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from " + table_name, null);
User user2 = new User();
return user2;
}
您正在从数据库中获取数据并创建新的User对象并返回此空对象。
尝试此代码或根据需要有所不同。
public User get_data() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from " + table_name, null);
if ((mCursor.moveToFirst()) || mCursor.getCount() !=0){
User user2 = new User();
user2.setUser_name(cursor.getString(0));
user2.setEmail(cursor.getString(1));
user2.setPassword(cursor.getString(2));
cursor.close();
}
return user2;
}