我要两张桌子"所有者"和"用户"选择第一个单选按钮时,它将数据保存在用户表上,选择第二个单选按钮数据时将保存在所有者表中
数据库助手类,其中包含用于创建用户表和所有者表的函数,以及用于在表中插入数据的insertdataUse insertdataOwner函数
这是我的单一页面,其中我们有两个函数adddatauser到user和adddataowner
public class SinGUP extends AppCompatActivity {
Databasehelper myDB; EditText et_name,et_pnumber,et_email,et_password; private static Button btnsignup; String name,pnumber,email,password; RadioButton r1,r2; RadioGroup rg; public int x; TextView loginbtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sin_gup); myDB = new Databasehelper(this); et_name=(EditText)findViewById(R.id.editText_pname); et_pnumber=(EditText)findViewById(R.id.editText_pnumber); et_password=(EditText)findViewById(R.id.editText_password); et_email=(EditText)findViewById(R.id.editText_email1); r1=(RadioButton)findViewById(R.id.radioButton_Owner); r2=(RadioButton)findViewById(R.id.radioButton_user); loginbtn=(TextView)findViewById(R.id.textView_login); //TypeOfUser(); loginbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i= new Intent(SinGUP.this,MainActivity.class); startActivity(i); } }); btnsignup=(Button)findViewById(R.id.button_singup); btnsignup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { singup(); } }); } public void singup() { initializes(); if(!Validate()) { Toast.makeText(SinGUP.this,"sigup can't be done",Toast.LENGTH_LONG).show(); } else { rg = (RadioGroup) findViewById(R.id.radiogroup); int selectOptId = rg.getCheckedRadioButtonId(); r1 = (RadioButton) findViewById(selectOptId); Toast.makeText(getApplicationContext(), r1.getText(), Toast.LENGTH_SHORT).show(); if (r1.getText() == "radioButton_user") Adddatauser(); if (r1.getText() == "radioButton_owner") AdddataOwner(); } } public void initializes() { name=et_name.getText().toString().trim(); pnumber=et_pnumber.getText().toString().trim(); email=et_email.getText().toString().trim(); password=et_password.getText().toString().trim(); } public boolean Validate() { boolean valid=true; if(name.isEmpty()||name.length()<7){ et_name.setError("please enter the valid name"); valid=false;} if(pnumber.isEmpty()||pnumber.length()<10){ et_pnumber.setError("please enter the valid phone number"); valid=false;} if(password.isEmpty()||password.length()<7){ et_password.setError(" password length 8"); valid=false;} if(email.isEmpty()||!isValidEmail(email)){ et_name.setError("please enter the valid email"); valid=false;} return valid; }
public boolean isValidEmail(String email) { String EMAIL_PATTERN = "^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)@"+"[A-Za-z0-9-]+(\.[A-Za-z0-9]+)(\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN); Matcher matcher = pattern.matcher(email); return matcher.matches(); } //function to add owner detail public void AdddataOwner(){ boolean inserData = myDB.insertdataOwner(name, pnumber, password, email); // boolean insertDataU = myDB.insertdataUser(name. getText().toString(), pnumber.getText().toString(), password.getText().toString(), email.getText().toString()); if (inserData == true) Toast.makeText(SinGUP.this, "data inserted owner", Toast.LENGTH_SHORT).show(); else Toast.makeText(SinGUP.this, "data canot be inserted owner", Toast.LENGTH_SHORT).show(); } //function to add user detail public void Adddatauser(){ boolean insertDataU = myDB.insertdataUser(name, pnumber, password, email); if (insertDataU==true) Toast.makeText(SinGUP.this, "data inserted user", Toast.LENGTH_SHORT).show(); else Toast.makeText(SinGUP.this, "data canot be inserted user", Toast.LENGTH_SHORT).show(); }
答案 0 :(得分:0)
更改以下条件
if (r1.getText() == "radioButton_user")
Adddatauser();
if (r1.getText() == "radioButton_owner")
AdddataOwner();
到
if (r1.getText().equalsIgnoreCase("radioButton_user")) {
Adddatauser();
} else if (r1.getText().equalsIgnoreCase("radioButton_owner")) {
AdddataOwner();
}