My app has a list of questions.When the functoion getfirstquestion is called,it randomly choses a question,and after that it randomly puts texts on each of the 4 buttons(one of them is the answer to the question).
Everytime a button is pressed,the function checkanswer verifies if the button text equals with the right answer to the right question.If it does,it removes the current question,and calls the getfirstquestion() function to change the question.
The program runs ok,however nothing happens when I press the correct answer button.Can anyone tell me why,and give me a solution? I’m searching the code since 2 hours ago….
public class startgame extends Activity implements OnClickListener{
final Random rgenerator = new Random();
List<String> questionss1 = new ArrayList<String>();
String thequestion;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level1);
questionss1.add("Who is the actual CEO at Apple?");
questionss1.add("Who is the actual CEO at Microsoft?");
questionss1.add("Who is the actual CEO at Google?");
getfirstquestion();
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(this);
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(this);
Button button4 = (Button)findViewById(R.id.button4);
button4.setOnClickListener(this);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.button1:
Button button1 = (Button)findViewById(R.id.button1);
checkanswer((String) button1.getText());
case R.id.button2:
Button button2 = (Button)findViewById(R.id.button2);
checkanswer((String) button2.getText());
case R.id.button3:
Button button3 = (Button)findViewById(R.id.button2);
checkanswer((String) button3.getText());
case R.id.button4:
Button button4 = (Button)findViewById(R.id.button2);
checkanswer((String) button4.getText());
}
}
public int checkanswer(String buttontext) {
if (thequestion.equals("Who is the actual CEO at Apple?") && buttontext == "Tim Cook"){
questionss1.remove("Who is the actual CEO at Apple?");
getfirstquestion();
}
if (thequestion.equals("Who is the actual CEO at Microsoft?") && buttontext == "Steve Ballmer"){
questionss1.remove("Who is the actual CEO at Microsoft?");
getfirstquestion();
}
if (thequestion.equals("Who is the actual CEO at Google?") && buttontext == "Eric Schmidt"){
questionss1.remove("Who is the actual CEO at Google?");
getfirstquestion();
}
return 0;
}
public void getfirstquestion(){
//create the buttons
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
thequestion = questionss1.get(rgenerator.nextInt(questionss1.size()));
TextView question = (TextView)findViewById(R.id.textView1);
question.setText(thequestion);
questionss1.remove(thequestion);
if (thequestion.equals("Who is the actual CEO at Apple?")){
List<String> questions1res = new ArrayList<String>();
questions1res.add("Eric Schmidt");
questions1res.add("Steven Sinofsky");
questions1res.add("Tim Cook");
questions1res.add("Steve Ballmer");
button1.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button1.getText());
button2.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button2.getText());
button3.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button3.getText());
button4.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button4.getText());
}
if (thequestion.equals("Who is the actual CEO at Microsoft?")){
List<String> questions1res = new ArrayList<String>();
questions1res.add("Eric Schmidt");
questions1res.add("Steven Sinofsky");
questions1res.add("Tim Cook");
questions1res.add("Steve Ballmer");
button1.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button1.getText());
button2.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button2.getText());
button3.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button3.getText());
button4.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button4.getText());
}
if (thequestion.equals("Who is the actual CEO at Google?")){
List<String> questions1res = new ArrayList<String>();
questions1res.add("Eric Schmidt");
questions1res.add("Steven Sinofsky");
questions1res.add("Tim Cook");
questions1res.add("Steve Ballmer");
button1.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button1.getText());
button2.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button2.getText());
button3.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button3.getText());
button4.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button4.getText());
}
}
}
button1.setOnClickListener(this);
This code is used to set an OnClickListener to a button. You need to instantiate an OnClickListener in your class and use that. Passing in (this) will pass in the activity. The button clicks will do nothing in this case.
Something like this:
Try that, let me know how it works.