当我按下提交按钮时,相同的活动会重新打开(清除所有选中的单选按钮)。仅当用户已注册时才会发生这种情况。
当用户是新用户时使用firebase,然后打开新的活动(MatchFind.class),这是正确的。
代码的主要部分
bSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference current_user_db = mDatabase.child(user_id);
int id1 = r1.getCheckedRadioButtonId();
int id2 = r2.getCheckedRadioButtonId();
int id3 = r3.getCheckedRadioButtonId();
int id4 = r4.getCheckedRadioButtonId();
int id5 = r5.getCheckedRadioButtonId();
int id6 = r6.getCheckedRadioButtonId();
int id7 = r7.getCheckedRadioButtonId();
int id8 = r8.getCheckedRadioButtonId();
int id9 = r9.getCheckedRadioButtonId();
int id10 = r10.getCheckedRadioButtonId();
if (id1 == -1 || id2 == -2 || id3 == -1 || id4 == -1 || id5 == -1 || id6 == -1 || id7 == -1
|| id8 == -1 || id9 == -1 || id10 == -1) {
Toast.makeText(Questions.this, "Please Answer All The Questions", Toast.LENGTH_SHORT).show();
} else {
rb1 = (RadioButton) findViewById(id1);
rb2 = (RadioButton) findViewById(id2);
rb3 = (RadioButton) findViewById(id3);
rb4 = (RadioButton) findViewById(id4);
rb5 = (RadioButton) findViewById(id5);
rb6 = (RadioButton) findViewById(id6);
rb7 = (RadioButton) findViewById(id7);
rb8 = (RadioButton) findViewById(id8);
rb9 = (RadioButton) findViewById(id9);
rb10 = (RadioButton) findViewById(id10);
getAnswers();
/*for (int i = 1; i < 11; i++) {
current_user_db.child("answer"+i).setValue(ans[i]);
}*/
current_user_db.child("answer"+1).setValue(ans[1]);
current_user_db.child("answer"+2).setValue(ans[2]);
current_user_db.child("answer"+3).setValue(ans[3]);
current_user_db.child("answer"+4).setValue(ans[4]);
current_user_db.child("answer"+5).setValue(ans[5]);
current_user_db.child("answer"+6).setValue(ans[6]);
current_user_db.child("answer"+7).setValue(ans[7]);
current_user_db.child("answer"+8).setValue(ans[8]);
current_user_db.child("answer"+9).setValue(ans[9]);
current_user_db.child("answer"+10).setValue(ans[10]);
Intent intent = new Intent(Questions.this, MatchFind.class);
startActivity(intent);
}
}
});
完整代码
public class Questions extends AppCompatActivity {
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private Button bSubmit;
private RadioGroup r1, r2, r3, r4, r5, r6, r7, r8, r9, r10;
private RadioButton rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8, rb9, rb10;
private String ans[] = new String[11];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
bSubmit = (Button) findViewById(R.id.bSubmit);
r1 = (RadioGroup) findViewById(R.id.rGroupQ1);
r2 = (RadioGroup) findViewById(R.id.rGroupQ2);
r3 = (RadioGroup) findViewById(R.id.rGroupQ3);
r4 = (RadioGroup) findViewById(R.id.rGroupQ4);
r5 = (RadioGroup) findViewById(R.id.rGroupQ5);
r6 = (RadioGroup) findViewById(R.id.rGroupQ6);
r7 = (RadioGroup) findViewById(R.id.rGroupQ7);
r8 = (RadioGroup) findViewById(R.id.rGroupQ8);
r9 = (RadioGroup) findViewById(R.id.rGroupQ9);
r10 = (RadioGroup) findViewById(R.id.rGroupQ10);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mAuth = FirebaseAuth.getInstance();
final String user_id = mAuth.getCurrentUser().getUid();
bSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference current_user_db = mDatabase.child(user_id);
int id1 = r1.getCheckedRadioButtonId();
int id2 = r2.getCheckedRadioButtonId();
int id3 = r3.getCheckedRadioButtonId();
int id4 = r4.getCheckedRadioButtonId();
int id5 = r5.getCheckedRadioButtonId();
int id6 = r6.getCheckedRadioButtonId();
int id7 = r7.getCheckedRadioButtonId();
int id8 = r8.getCheckedRadioButtonId();
int id9 = r9.getCheckedRadioButtonId();
int id10 = r10.getCheckedRadioButtonId();
if (id1 == -1 || id2 == -2 || id3 == -1 || id4 == -1 || id5 == -1 || id6 == -1 || id7 == -1
|| id8 == -1 || id9 == -1 || id10 == -1) {
Toast.makeText(Questions.this, "Please Answer All The Questions", Toast.LENGTH_SHORT).show();
} else {
rb1 = (RadioButton) findViewById(id1);
rb2 = (RadioButton) findViewById(id2);
rb3 = (RadioButton) findViewById(id3);
rb4 = (RadioButton) findViewById(id4);
rb5 = (RadioButton) findViewById(id5);
rb6 = (RadioButton) findViewById(id6);
rb7 = (RadioButton) findViewById(id7);
rb8 = (RadioButton) findViewById(id8);
rb9 = (RadioButton) findViewById(id9);
rb10 = (RadioButton) findViewById(id10);
getAnswers();
/*for (int i = 1; i < 11; i++) {
current_user_db.child("answer"+i).setValue(ans[i]);
}*/
current_user_db.child("answer"+1).setValue(ans[1]);
current_user_db.child("answer"+2).setValue(ans[2]);
current_user_db.child("answer"+3).setValue(ans[3]);
current_user_db.child("answer"+4).setValue(ans[4]);
current_user_db.child("answer"+5).setValue(ans[5]);
current_user_db.child("answer"+6).setValue(ans[6]);
current_user_db.child("answer"+7).setValue(ans[7]);
current_user_db.child("answer"+8).setValue(ans[8]);
current_user_db.child("answer"+9).setValue(ans[9]);
current_user_db.child("answer"+10).setValue(ans[10]);
Intent intent = new Intent(Questions.this, MatchFind.class);
startActivity(intent);
}
}
});
}
private void getAnswers() {
if (rb1.getText().toString().equals("Long Term Relationship"))
ans[1] = "a";
else
ans[1] = "b";
if (rb2.getText().toString().equals("Yes"))
ans[2] = "a";
else
ans[2] = "b";
if (rb3.getText().toString().equals("Yes"))
ans[3] = "a";
else
ans[3] = "b";
if (rb4.getText().toString().equals("Bollywood Movies"))
ans[4] = "a";
else
ans[4] = "b";
if (rb5.getText().toString().equals("Yes"))
ans[5] = "a";
else
ans[5] = "b";
if (rb6.getText().toString().equals("Yes"))
ans[6] = "a";
else
ans[6] = "b";
if (rb7.getText().toString().equals("Yes"))
ans[7] = "a";
else
ans[7] = "b";
if (rb8.getText().toString().equals("Yes"))
ans[8] = "a";
else
ans[8] = "b";
if (rb9.getText().toString().equals("Yes"))
ans[9] = "a";
else
ans[9] = "b";
if (rb10.getText().toString().equals("Yes"))
ans[10] = "a";
else
ans[10] = "b";
}
FindMatch.java
public class MatchFind extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_find);
}
}
答案 0 :(得分:0)
添加此标志,FLAG_ACTIVITY_CLEAR_TASK,并在开始活动之前调用finish()。执行干净,然后重新启动,最好使无效并重新启动以清除缓存。
答案 1 :(得分:0)
您是否对登录用户使用firebase身份验证。您需要在On create中创建Firebase AuthStateListener()。并检查用户是否已登录或不在那里。如果用户签名的身份验证状态已更改,您可以点击那里按钮。