Я следовал за другим сообщением на этом сайте о том, как перетасовывать вопросы и ответы для приложения викторины, но мне нужна помощь в (а) отображении вопроса в текстовом просмотре и (б) проверке ответа из текста редактирования, так как я не знать, как вызывать их из перемешанного массива.
Основной файл активности -
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button submit_btn, next_btn;
TextView scoremm6, question;
EditText answer;
String manswer;
public TextView getScoreT2() {
return scoremm6;
}
private int mScore = 0;
private boolean done;
private int QuestionNo = 0;
private int mQuestionNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerquestion);
findViewById(R.id.submit_btn).setEnabled(true);
final Button enter_btn = (Button) findViewById(R.id.submit_btn);
answer = (EditText) findViewById(R.id.answer);
scoremm6 = (TextView) findViewById(R.id.scoremm6);
question = (TextView) findViewById(R.id.question);
scoremm6.setText("Score: " + mScore);
List<Questions> questions = new ArrayList<Questions>();
questions.add(
new Questions(
"3 x 5",
"15"
)
);
questions.add(
new Questions(
"4 x 5",
"20"
)
);
questions.add(
new Questions(
"5 x 5",
"25"
)
);
questions.add(
new Questions(
"6 x 5",
"30"
)
);
questions.add(
new Questions(
"7 x 5",
"35"
)
);
questions.add(
new Questions(
"8 x 5",
"40"
)
);
questions.add(
new Questions(
"9 x 5",
"45"
)
);
questions.add(
new Questions(
"10 x 5",
"50"
)
);
questions.add(
new Questions(
"11 x 5",
"55"
)
);
questions.add(
new Questions(
"12 x 5",
"60"
)
);
questions.add(
new Questions(
"2 x 4",
"8"
)
);
questions.add(
new Questions(
"3 x 4",
"12"
)
);
questions.add(
new Questions(
"4 x 4",
"16"
)
);
questions.add(
new Questions(
"5 x 4",
"15"
)
);
Collections.shuffle(questions);
// Здесь мне нужна помощь, чтобы получить правильный ответ из перетасованного массива
Button Submit = (Button) findViewById(R.id.submit_btn);
Submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (done == false) {
String answer = ((EditText) findViewById(R.id.answer)).getText().toString();
String[] answers1 = getString(new ArrayList<Questions>manswer;
String correctanswer = answers1[QuestionNo];
//gets the answer and correct answer from the edit text and strings.xml respectively
correctanswer = correctanswer;
answer = answer;
if (answer.equals(correctanswer)) {
mScore++;
findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerbuttonscorrect);
scoremm6.setText("Score: " + mScore);
enter_btn.setEnabled(false);
Toast.makeText(getApplicationContext(), "Correct.",
Toast.LENGTH_SHORT).show();
} else {
findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerbuttonsincorrect);
enter_btn.setEnabled(false);
}
}
}
});
// Мне также нужна помощь, чтобы перейти к другому вопросу
Button Next2 = (Button) findViewById(R.id.next_btn);
Next2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String[] mquestions = getString(new ArrayList<Questions>);
if (QuestionNo < (mquestions.length -1)) {
QuestionNo = QuestionNo + 1;
findViewById(R.id.answer).setBackgroundResource(R.drawable.rounded_cornerquestion);
enter_btn.setEnabled(true);
answer.getText().clear();
TextView t = (TextView) findViewById(R.id.question);
t.setText(mquestions[QuestionNo]);
}
{
if (QuestionNo == (questions.length)) {
enter_btn.setEnabled(false);
Toast.makeText(getApplicationContext(), "Test Completed",
Toast.LENGTH_LONG).show();
}
}
}
});
}
}