我无法获得四个多项选择按钮和问题textView来显示文本。所发生的事情是有一个意图转移到选定的类别"谁"或"什么"。该类别与" who"和"什么"如果声明。如果它是其中之一,那么它应该发布问题并贯穿十个问题。我在if语句中使用了一个switch case来帮助区分点击了什么按钮并继续前进。当我运行模拟器时,它会通过四个多选按钮进入屏幕,但文本或文本视图上没有显示任何内容。
public class QuestionActivity extends AppCompatActivity {
List<QuestionsTable> whoQuesList;
List<QuestionsTable> whatQuesList;
int score=0;
int qid=0;
QuestionsTable currentWhatQ;
QuestionsTable currentWhoQ;
Button btnA, btnB, btnC, btnD;
Button butSkip;
TextView txtQuestion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
butSkip = (Button) findViewById(R.id.btnSkip);
MySQLiteHelper db = new MySQLiteHelper(this);
whoQuesList = db.getAllWhos();
whatQuesList = db.getAllWhos();
currentWhatQ = whatQuesList.get(qid);
currentWhoQ = whoQuesList.get(qid);
}
public void onClickQuestion(View txtViewQuestion) {
Bundle d = getIntent().getExtras();
int c = d.getInt("category");
String category = Integer.toString(c);
if ("Who".equals(category)) {
txtQuestion = (TextView) findViewById(R.id.txtViewQuestion);
btnA = (Button) findViewById(R.id.btnOptionA);
btnB = (Button) findViewById(R.id.btnOptionB);
btnC = (Button) findViewById(R.id.btnOptionC);
btnD = (Button) findViewById(R.id.btnOptionD);
setQuestionView();
switch (txtViewQuestion.getId()) {
case btnOptionA:
if (currentWhoQ.getAnswer().equals(btnA.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case btnOptionB:
if (currentWhoQ.getAnswer().equals(btnB.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case btnOptionC:
if (currentWhoQ.getAnswer().equals(btnC.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case R.id.btnOptionD:
if (currentWhoQ.getAnswer().equals(btnD.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
}
}
if ("What".equals(category)) {
MySQLiteHelper db = new MySQLiteHelper(this);
whoQuesList = db.getAllWhos();
whatQuesList = db.getAllWhos();
currentWhatQ = whatQuesList.get(qid);
currentWhoQ = whoQuesList.get(qid);
txtQuestion = (TextView) findViewById(R.id.txtViewQuestion);
btnA = (Button) findViewById(R.id.btnOptionA);
btnB = (Button) findViewById(R.id.btnOptionB);
btnC = (Button) findViewById(R.id.btnOptionC);
btnD = (Button) findViewById(R.id.btnOptionD);
setWhatQuestionView();
switch (txtViewQuestion.getId()) {
case btnOptionA:
if (currentWhatQ.getAnswer().equals(btnA.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case btnOptionB:
if (currentWhatQ.getAnswer().equals(btnB.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case btnOptionC:
if (currentWhatQ.getAnswer().equals(btnC.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case R.id.btnOptionD:
if (currentWhatQ.getAnswer().equals(btnD.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
}
}
}
public void setQuestionView() {
txtQuestion.setText(currentWhoQ.getQuestion());
btnA.setText(currentWhoQ.getMultipleChoiceA());
btnB.setText(currentWhoQ.getMultipleChoiceB());
btnC.setText(currentWhoQ.getMultipleChoiceC());
btnD.setText(currentWhoQ.getMultipleChoiceD());
qid++;
}
public void setWhatQuestionView() {
txtQuestion.setText(currentWhatQ.getQuestion());
btnA.setText(currentWhatQ.getMultipleChoiceA());
btnB.setText(currentWhatQ.getMultipleChoiceB());
btnC.setText(currentWhatQ.getMultipleChoiceC());
btnD.setText(currentWhatQ.getMultipleChoiceD());
qid++;
}
答案 0 :(得分:0)
Bundle d = getIntent().getExtras();
int c = d.getInt("category");
String category = Integer.toString(c);
if ("Who".equals(category)) {
c的价值是多少?它怎么能平等?#34;谁&#34;或&#34;什么&#34;?
和其他人一样,使用switch语句中按钮的完整ID。
case R.id.btnOptionA:
而不是
case btnOptionA:
答案 1 :(得分:0)
不确定,你的txtViewQuestion是什么类型的数据...... Switch语句与原始数据类型完美配合......
如果您尝试调用Button变量的名称,这将不起作用,因为程序仅适用于内存地址,因此Java将无法为您提供对象的名称(如果它是一个Button)
要解决此问题,您可以将参数传递给方法(例如int) 0表示第一个按钮 1表示第二个按钮 2为第三... 然后你可以&#34; Switch&#34;这个参数
答案 2 :(得分:0)
什么是btnOptionA,btnOptionB和btnOptionC? txtViewQuestion.getId()返回一个Integer,在case标签中你应该使用R.id.btnOptionA,R.id.btnOptionB,R.id.btnOptionC ......