gpt4 book ai didi

android - 当我输入相同的用户名时它给了我 "Wrong input"?

转载 作者:行者123 更新时间:2023-11-30 05:04:05 25 4
gpt4 key购买 nike

我正在尝试使用 SQL 编写用于登录和注册的 android 代码,当我运行该应用程序并尝试使用我注册时使用的相同用户名登录时,它不会转到主页,但会执行 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("");

Login_SQLHelper

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

最佳答案

问题出在这个方法上。

public User get_data() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from " + table_name, null);
User user2 = new User();
return user2;
}

您正在从数据库中获取数据并创建新的用户对象并返回此空对象。

尝试此代码或根据需要做出一些改变。

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

关于android - 当我输入相同的用户名时它给了我 "Wrong input"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54863683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com