Reputation: 71
I want to set my quiz to only show three questions , and not proceeding to the next one until it gets the correct answer. Here's my current code
My application goes like this by the way,
Button to check if answer is correct/incorrect
public class ArrayAct extends Activity {
private Button doneBtn;
private EditText text;
private TextView textView;
private String [] mArray;
private String [] mArray1;
private int generatedIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arrayact);
textView = (TextView)findViewById(R.id.txtView);
doneBtn = (Button)findViewById(R.id.doneBtn);
text = (EditText)findViewById(R.id.txtFld);
mArray = getResources().getStringArray(R.array.Answers);
mArray1 = getResources().getStringArray(R.array.Questions);
doneBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mArray[generatedIndex].equals(text.getText().toString()))
Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "Incorrect.Please try again.", Toast.LENGTH_SHORT).show();
}
});
Random random = new Random();
generatedIndex = random.nextInt(mArray1.length);
textView.setText(mArray1[generatedIndex]);
}
}
Upvotes: 1
Views: 86
Reputation: 24848
Try this way,hope this will help you to solve your problem.
public class ArrayAct extends Activity {
private Button doneBtn;
private EditText text;
private TextView textView;
private String [] mArray;
private String [] mArray1;
private int generatedIndex;
private HashMap<String,String> questionMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arrayact);
textView = (TextView)findViewById(R.id.txtView);
doneBtn = (Button)findViewById(R.id.doneBtn);
text = (EditText)findViewById(R.id.txtFld);
mArray = getResources().getStringArray(R.array.Answers);
mArray1 = getResources().getStringArray(R.array.Questions);
questionMap = new HashMap<String, String>();
doneBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(doneBtn.getText().equals("Close")){
finish();
}else{
if (mArray[generatedIndex].equals(text.getText().toString())){
Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
if(questionMap.size()==3){
Toast.makeText(getApplicationContext(), "Your test is over!!!", Toast.LENGTH_SHORT).show();
doneBtn.setText("Close");
}else{
text.setText("");
prepareQuestion();
}
}else{
Toast.makeText(getApplicationContext(), "Incorrect.Please try again.", Toast.LENGTH_SHORT).show();
}
}
}
});
prepareQuestion();
}
private void prepareQuestion(){
Random random = new Random();
do{
generatedIndex = random.nextInt(mArray1.length);
}while(questionMap.containsKey(String.valueOf(generatedIndex)));
questionMap.put(String.valueOf(generatedIndex),"");
textView.setText(mArray1[generatedIndex]);
}
}
Upvotes: 1
Reputation: 2075
doneBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mArray[generatedIndex].equals(text.getText().toString())) {
Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
if(arrayOfUsedIndexes.size() == 3) {
//Some reaction for all 3 questions answered correctly
}
else {
int newGeneratedIndex;
do {
newGeneratedIndex = random.nextInt(mArray1.length);
} while(arrayOfUsedIndexes.contains(newGeneratedIndex))
textView.setText(mArray1[newGeneratedIndex]);
arrayOfUsedIndexes.add(newGeneratedIndex);
}
}
else {
Toast.makeText(getApplicationContext(), "Incorrect.Please try again.", Toast.LENGTH_SHORT).show(); }
}
});
Random random = new Random();
generatedIndex = random.nextInt(mArray1.length);
arrayOfUsedIndexes.add(generatedIndex);
textView.setText(mArray1[generatedIndex]);
Upvotes: 0