I can successfully display the first problem from the text file, however I am having trouble accepting that answer and then displaying the second problem. I used a for loop in order to read lines from the text file. I also have an onClick listener.
My major problem is, how do I have the for loop wait until the onClick listener is hit before looping into the second problem.
Here's the relevant code:
public class MathTestActivity extends AppCompatActivity {
String correctAnswer;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mathtest);
TextView mathProblem = (TextView) findViewById(R.id.mathProblem);
final EditText mathAnswer = (EditText) findViewById(R.id.mathAnswer);
//Stying for the question text
mathProblem.setTextSize(40);
mathProblem.setTextColor(Color.rgb(0, 0, 0));
//Try to read the problem and answers text file
try {
InputStream is = this.getResources().openRawResource(R.raw.mediummath);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
int n = 20; /*How many rows this test has*/
for (i = 0; i < n; i++) {
if ((line = reader.readLine()) != null)
mathProblem.setText(line);
if ((line = reader.readLine()) != null)
correctAnswer = line;
}
} catch (IOException e) {
e.printStackTrace();
}
Button enterButton = (Button) findViewById(R.id.enterButton);
enterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int correctcount = 0;
String answer = mathAnswer.toString() ;//need to turn answer into string Integer.parseInt(mathAnswer.getText().toString());
if (answer == correctAnswer){
Toast.makeText(MathTestActivity.this,
R.string.correct_toast,
Toast.LENGTH_SHORT).show();
correctcount++;
i++;
}
else{
Toast.makeText(MathTestActivity.this,
R.string.incorrect_toast,
Toast.LENGTH_SHORT).show();
i++;
}
}
});
//Add layout for this activity
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mtestcontent);
layout.addView(mathProblem);
layout.addView(mathAnswer);
}
}
As you can see, it's the logic inside the "try" portion that I'm having difficulty with. Can someone help me with the logic here?