0

I am currently trying to make an order game. The user had to press the buttons in the order given. How ever I am having some problem with the else statement. This is my code.

private OnClickListener click2 = new OnClickListener() { List<Integer> mClickedButtonIds = new ArrayList<Integer>(); @Override public void onClick(View v) { mClickedButtonIds.add(v.getId()); if (mClickedButtonIds.size() > 4 ) { mClickedButtonIds.remove(0); int[] mDesiredOrder = new int[] { R.id.imageButton1, R.id.imageButton2, R.id.imageButton3, R.id.imageButton4 }; if (mClickedButtonIds.get(0) == mDesiredOrder[0] && mClickedButtonIds.get(1) == mDesiredOrder[1] && mClickedButtonIds.get(2) == mDesiredOrder[2] && mClickedButtonIds.get(3) == mDesiredOrder[3] ) { tv1.setText("Correct"); } else { tv1.setText("Wrong"); } } } }; 

If I play the game for the first time, the else statement work perfectly. But if I play the game again without closing the application, the else statement start to have problem. Every single button I press will come out the else statement which make my tv1 to set text as Wrong. But the ifstatement still work perfectly. Means that although it keep on set text as Wrong but if I click the final button of the correct order it will set text as Correct. Is there any solution for this?

1
  • private OnClickListener click2 = new OnClickListener() { List<Integer> mClickedButtonIds = new ArrayList<Integer>(); do this in onResume(), not onCreate(). Commented Jan 4, 2014 at 8:17

2 Answers 2

2

Probably you didn't create a new OnClickListener every game, so the ArrayList was not cleared.

Sign up to request clarification or add additional context in comments.

7 Comments

okay thanks a lot after clearing the arraylist it become good. However I still have some problem. This game only need to click 4 time, however I need to click it 5 time in order for it to work if I play the game for the 2nd time after clearing the arraylist. Cause if I didn't clear the arraylist, the 1st time, I need to click 5 timse but 2nd time, I only need to click 4 times. Is there any method to solve this.
Does not work. If i change to 3, the word Correct did not come out even if I press it in order. I think is due to mClickedButtonIds.get(3) == mDesiredOrder[3] requirement is not fulfilled.
I guess I will just need to add another button for the user to click for nothing. Anyway thanks for helping me to solve my arraylist problem.
I solved my problem. I looks like I need to change to ` if (mClickedButtonIds.size() >= 4 )` and remove mClickedButtonIds.remove(0);
|
0

Try Else If for the second block rather than just " Else "

1 Comment

but if I use else if then what is the condition for it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.