gpt4 book ai didi

java - 根据单选按钮值将值存储在 sqlite 数据库的多个表中

转载 作者:太空宇宙 更新时间:2023-11-04 12:17:38 25 4
gpt4 key购买 nike

数据库中创建了两个名为“adopter”和“donor”的表。当单击“作为捐赠者”单选按钮时,注册表的值应存储在正常工作的捐赠者表中。问题是,当单击“作为采用者”单选按钮时,值将存储在捐赠者表中,而 id(主键)将单独输入到采用者表中。这里的"is"单选按钮是“作为捐赠者”,“否”单选按钮是“作为收养者”,我需要你的帮助。

    findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String email = mail.getText().toString();
final String name1 = name.getText().toString();
final String pass = password.getText().toString();
final String pass1 = password1.getText().toString();
int id;
id = student.getCheckedRadioButtonId();
boolean check;
check = stud.isChecked();
if (!isValidName(name1)) {
name.setError("Invalid name");
}
if (isValidName(name1) && !isValidEmail(email)) {
mail.setError("Invalid Email");
}
if (isValidName(name1) && isValidEmail(email) && !isValidPassword(pass)) {
password.setError("Invalid password");
}
if (isValidName(name1) && isValidEmail(email) && isValidPassword(pass)) {

if (!isValidPassword(pass1)) {
password1.setError("Invalid password");
}
}
if (isValidPassword(pass) != isValidPassword(pass1)) {
password1.setError("The given passwords does not match ");
}

if (isValidName(name1) && isValidEmail(email) && isValidPassword(pass) && isValidPassword(pass) == isValidPassword(pass1) && (id == -1)) {
Toast toast;
toast = makeText(Register.this, "Click any one of the option", Toast.LENGTH_SHORT);
toast.show();
}
if (isValidName(name1) && isValidEmail(email) && isValidPassword(pass) && (id != -1) && isValidPassword(pass) == isValidPassword(pass1) && (check == false)) {
Toast msg1;
msg1 = makeText(Register.this, "You can not register without clicking this checkbox", Toast.LENGTH_SHORT);
msg1.show();
}
if (isValidName(name1) && isValidEmail(email) && isValidPassword(pass) && isValidPassword(pass) == isValidPassword(pass1) && (check == true) && (yes.isEnabled()==true)) {
Toast msg2;
msg2 = makeText(Register.this, "Step 1 of registration is completed", Toast.LENGTH_SHORT);
msg2.show();
Intent launch;
launch = new Intent(Register.this, donor.class);
startActivity(launch);
donor_insert(view);

if (isValidName(name1) && isValidEmail(email) && isValidPassword(pass) && isValidPassword(pass) == isValidPassword(pass1) && (check == true) && (no.isEnabled()==true)){
Toast msg3;
msg3 = makeText(Register.this, "Step 1 of registration is completed", Toast.LENGTH_SHORT);
msg3.show();
Intent adopter;
adopter = new Intent(Register.this, adopter.class);
startActivity(adopter);
adopter_insert(view);
}

}

}
});
}
private boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^(?=.*[a-z]).(?=\\S+$).{4,}$";

Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
private boolean isValidName(String name) {
String name1 = "^(?=.*[a-z]).{1,}$";
Pattern pattern1 = Pattern.compile(name1);
Matcher matcher1 = pattern1.matcher(name);
return matcher1.matches();
}

// validating password with retype password
private boolean isValidPassword(String pass) {
if (pass != null && pass.length() > 6) {
return true;
}
return false;
}
public void adopter_insert(View view) {
UserInputs adopt = new UserInputs(name.getText().toString(), mail.getText().toString(), password.getText().toString());
dbHandler.adopter_insert(adopt);
name.setText("");
mail.setText("");
password.setText("");
}
public void donor_insert(View view) {
UserInputs donate = new UserInputs(name.getText().toString(), mail.getText().toString(), password.getText().toString());
dbHandler.donor_insert(donate);
name.setText("");
mail.setText("");
password.setText("");
}

}

{//Dbhandler.java

public DBhandler(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, DBNAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
String create_table = ("CREATE TABLE " + TABLE + "("
+ COLUMN_ID1 + " INTEGER PRIMARY KEY," + COLUMN_NAME
+ " TEXT," + COLUMN_MAIL + " TEXT, " + COLUMN_PASSWORD + " BLOB " + ")");
db.execSQL(create_table);
String create_table1 = ("CREATE TABLE " + TABLE1 + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_NAME
+ " TEXT," + COLUMN_MAIL + " TEXT, " + COLUMN_PASSWORD + " BLOB " + ")");
db.execSQL(create_table1);
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE);
onCreate(db);
db.execSQL("DROP TABLE IF EXISTS " + TABLE1);
onCreate(db);
}

public void adopter_insert(UserInputs inputs) {
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, inputs.getName());
values.put(COLUMN_MAIL, inputs.getMail());
values.put(COLUMN_PASSWORD, inputs.getPass());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE, null, values);
db.close();
}
public void donor_insert(UserInputs inputs) {
ContentValues values1 = new ContentValues();
values1.put(COLUMN_NAME, inputs.getName());
values1.put(COLUMN_MAIL, inputs.getMail());
values1.put(COLUMN_PASSWORD, inputs.getPass());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE1, null, values1);
db.close();
}

}

}

最佳答案

您在最后的 if 条件中看到您需要这样设置,因为当您的捐赠者被检查时,您的第一个 if 将被执行,如果您的收养者被检查,那么您的第二个 if 将被执行。我希望这会对您有所帮助。

if (isValidName(name1) && isValidEmail(email) && isValidPassword(pass)  && isValidPassword(pass) == isValidPassword(pass1) && (check == true) && (yes.isEnabled()==true)) 
{
Toast msg2;
msg2 = makeText(Register.this, "Step 1 of registration is completed", Toast.LENGTH_SHORT);
msg2.show();
Intent launch;
launch = new Intent(Register.this, donor.class);
startActivity(launch);
donor_insert(view);
}
if (isValidName(name1) && isValidEmail(email) && isValidPassword(pass) && isValidPassword(pass) == isValidPassword(pass1) && (check == true) && (no.isEnabled()==true))
{
Toast msg3;
msg3 = makeText(Register.this, "Step 1 of registration is completed", Toast.LENGTH_SHORT);
msg3.show();
Intent adopter;
adopter = new Intent(Register.this, adopter.class);
startActivity(adopter);
adopter_insert(view);
}

关于java - 根据单选按钮值将值存储在 sqlite 数据库的多个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39201741/

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