![](/img/trans.png)
[英]How to load set of question and answer to end user based on the admin action for quiz application
[英]Android quiz How to show the question based on the answer?
我目前正在做一個測驗應用程序,問題應該根據答案出來。 以下是我的代碼。 我一直在網上尋找各種選擇,但找不到一種方法來解決它而不會犯很多錯誤。 我的測驗活動的代碼如下。 謝謝。
測驗.java
public class quiz extends AppCompatActivity implements View.OnClickListener {
TextView questionNo, question;
RadioGroup rg;
Button quiz_nextBtn, quiz_PrevBtn;
RadioButton rb1, rb2;
int current_question = 0;
int next_question=0;
private quizAdapter[] questionBank = new quizAdapter[]{
new quizAdapter(R.string.q1, R.string.q1_a1, R.string.q1_a2),
new quizAdapter(R.string.q2, R.string.q2_a1, R.string.q2_a2),
new quizAdapter(R.string.q3, R.string.q3_a1, R.string.q3_a2),
new quizAdapter(R.string.q4, R.string.q4_a1, R.string.q4_a2),
new quizAdapter(R.string.q5, R.string.q5_a1, R.string.q5_a2),
new quizAdapter(R.string.q6, R.string.q6_a1, R.string.q6_a2),
new quizAdapter(R.string.q7, R.string.q7_a1, R.string.q7_a2)
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
rg = findViewById(R.id.quiz_answer);
rb1 = findViewById(R.id.rb1);
rb2 = findViewById(R.id.rb2);
questionNo = findViewById(R.id.quiz_questionNo);
question = findViewById(R.id.quiz_question);
quiz_nextBtn = findViewById(R.id.quiz_nextBtn);
quiz_PrevBtn = findViewById(R.id.quiz_PrevBtn);
quiz_nextBtn.setOnClickListener(this);
quiz_PrevBtn.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.quiz_nextBtn:
if(current_question<7){
current_question++;
if(current_question == 6){
question.setText("We are finish");
}
else {
if(current_question == 0 && rb1.isChecked()){
current_question = 1;
}
else if (current_question == 0 && rb2.isChecked()){
current_question = 4;
}
else if (current_question == 1 && rb1.isChecked()){
current_question = 2;
}
else if (current_question == 1 && rb2.isChecked()){
current_question = 3;
}
else if (current_question == 4 && rb1.isChecked()){
current_question = 5;
}
else if (current_question == 4 && rb2.isChecked()){
current_question = 6;
}
updateQuestion();
}
}
break;
case R.id.quiz_PrevBtn:
if (current_question > 0) {
current_question = (current_question - 1) % questionBank.length;
updateQuestion();
}
}
}
private void updateQuestion() {
Log.d("Current", "onClick: " + current_question);
question.setText(questionBank[current_question].getQuestion());
rb1.setText(questionBank[current_question].getRb1());
rb2.setText(questionBank[current_question].getRb2());
}
}
quizAdapter.java
public class quizAdapter {
private int question;
private int rb1;
private int rb2;
public quizAdapter(int question, int rb1, int rb2)
{
this.question = question;
this.rb1 = rb1;
this.rb2 = rb2;
}
public int getQuestion()
{
return question;
}
public int getRb1()
{
return rb1;
}
public int getRb2()
{
return rb2;
}
}
我認為更好的方法是使用 HashMap 並將測驗答案和問題設置為鍵值對,對於您嘗試實現的內容,您將有多個鍵(當前問題的值和選擇的選項)用於單個值(下一個要顯示的問題)。 一種方法是創建一個自定義 HashMap 如下所示
import java.util.HashMap;
import java.util.Map;
class Key<K1, K2> {
public K1 key1;
public K2 key2;
public Key(K1 key1, K2 key2) {
this.key1 = key1;
this.key2 = key2;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) return false;
if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) return false;
return true;
}
@Override
public int hashCode() {
int result = key1 != null ? key1.hashCode() : 0;
result = 31 * result + (key2 != null ? key2.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "[" + key1 + ", " + key2 + "]";
}
}
class Main
{
public static void main(String[] args) {
// Create a HashMap with Key as key
Map<Key, String> multiKeyMap = new HashMap<>();
// [key1, key2] -> value1
Key k12 = new Key("key1", "key2");
multiKeyMap.put(k12, "value1");
// [key3, key4] -> value2
Key k34 = new Key("key3", "key4");
multiKeyMap.put(k34, "value2");
// print multi-key map
System.out.println(multiKeyMap);
// print value corresponding to key1 and key2
System.out.println(multiKeyMap.get(k12));
}
}
更多解釋可以在這個鏈接中找到https://www.techiedelight.com/implement-map-with-multiple-keys-multikeymap-java/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.