2016-10-19 107 views
0

我正在編程我的第一個Android應用程序。

我想創建一個測驗應用程序。我有問題存儲在一個SQLite數據庫中,它們依次顯示。
用戶選擇其中一個答案(一個單選按鈕),然後點擊'下一個按鈕'並顯示下一個問題,等等。

以下代碼顯示我的活動文件,其中一個接一個地顯示每個問題,這是完美工作

顯示下一個數據庫條目


ACEActivity(老,工作版本)

public class ACEActivity extends Activity { 
List<Question> quesList; 
int score = 0; 
int qid = 0; 
Question currentQ; 
TextView txtQuestion; 
RadioButton rda, rdb, rdc, rdd; 
Button butNext; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_quiz); 

    DatabaseHelper db = new DatabaseHelper(this); 

    quesList = db.getAllACEQuestions(); 
    currentQ = quesList.get(qid); 
    txtQuestion = (TextView)findViewById(R.id.textView); 
    rda = (RadioButton)findViewById(R.id.radio0); 
    rdb = (RadioButton)findViewById(R.id.radio1); 
    rdc = (RadioButton)findViewById(R.id.radio2); 
    rdd = (RadioButton)findViewById(R.id.radio3); 
    butNext = (Button)findViewById(R.id.nextButton); 
    setQuestionView(); 

    butNext.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      RadioGroup grp = (RadioGroup)findViewById(R.id.radioGroup); 
      RadioButton answer = (RadioButton)findViewById(grp.getCheckedRadioButtonId()); 
      Log.d("yourans", currentQ.getANSWER() + " " + answer.getText()); 
      // If the correct answer was clicked display the next question 
      if(currentQ.getANSWER().equals(answer.getText())) { 
       currentQ = quesList.get(qid); 
       setQuestionView(); 
      } 
     } 
    }); 
} 

// Load the next question 
private void setQuestionView() { 
    txtQuestion.setText(currentQ.getQUESTION()); 
    rda.setText(currentQ.getOPTA()); 
    rdb.setText(currentQ.getOPTB()); 
    rdc.setText(currentQ.getOPTC()); 
    rdd.setText(currentQ.getOPTD()); 
    qid++; 
    } 
} 


在我的這個活動我加載另一個活動給用戶即時反饋,如果被點擊了正確或錯誤的答案的新版本。

顯示反饋活動後,我想返回到此活動並顯示下一個問題。

我試圖通過從反饋活動(ACECorrectActivity)傳遞問題ID到這個活動(ACEActivity)沒有任何成功。



我如何試圖解決這個問題:

ACEActivity(新版本,只是工作的第一個問題)

public class ACEActivity extends Activity { 
List<Question> quesList; 
int score = 0; 
int qid = 0; 
Question currentQ; 
TextView txtQuestion; 
RadioButton rda, rdb, rdc, rdd; 
Button checkBtn; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_quiz); 

    // THIS PART IS NEW ================================ 
    // Get the intent 
    Intent intent = getIntent(); 
    // Get the question id (if there are any extras) 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     int qid = extras.getInt("nextQuestionID"); 
    } else { 
     int qid = 0; 
    } 
    // ================================================== 

    DatabaseHelper db = new DatabaseHelper(this); 

    quesList = db.getAllACEQuestions(); 
    currentQ = quesList.get(qid); 
    txtQuestion = (TextView)findViewById(R.id.textView); 
    rda = (RadioButton)findViewById(R.id.radio0); 
    rdb = (RadioButton)findViewById(R.id.radio1); 
    rdc = (RadioButton)findViewById(R.id.radio2); 
    rdd = (RadioButton)findViewById(R.id.radio3); 
    checkBtn = (Button) findViewById(R.id.checkButton); 

    setQuestionView(qid); 

    checkBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup); 
      RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId()); 
      Log.d("yourans", currentQ.getANSWER() + " " + answer.getText()); 
      // THIS PART IS NEW AND WORKING FINE ================================ 
      // If the correct answer was clicked 
      if (currentQ.getANSWER().equals(answer.getText())) { 
       Intent intent = new Intent(ACEActivity.this, CorrectACEActivity.class); 
       startActivity(intent); 
      // If the wrong answer was clicked 
      } else { 
       Intent intent = new Intent(ACEActivity.this, FalseACEActivity.class); 
       startActivity(intent); 
      } 
     } 
    }); 
} 

private void setQuestionView() { 
    txtQuestion.setText(currentQ.getQUESTION()); 
    rda.setText(currentQ.getOPTA()); 
    rdb.setText(currentQ.getOPTB()); 
    rdc.setText(currentQ.getOPTC()); 
    rdd.setText(currentQ.getOPTD()); 
    qid++; 
    } 
} 

ACECorrectActivity(回饋活動加載,當選擇正確的答案並在ACEActivity中點擊下一個按鈕)

public class CorrectACEActivity extends Activity { 
List<Question> quesList; 
int score = 0; 
int qid = 0; 
Question currentQ; 
TextView txtQuestion; 
RadioButton rda, rdb, rdc, rdd; 
Button nextBtn; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_correct); 

    DatabaseHelper db = new DatabaseHelper(this); 

    quesList = db.getAllACEQuestions(); 
    currentQ = quesList.get(qid); 
    txtQuestion = (TextView) findViewById(R.id.textView); 
    rda = (RadioButton) findViewById(R.id.radio0); 
    rdb = (RadioButton) findViewById(R.id.radio1); 
    rdc = (RadioButton) findViewById(R.id.radio2); 
    rdd = (RadioButton) findViewById(R.id.radio3); 
    nextBtn = (Button) findViewById(R.id.nextButton); 

    // Set colors according to correct answer 
    rda.setBackgroundColor(Color.RED); 
    rdb.setBackgroundColor(Color.RED); 
    rdc.setBackgroundColor(Color.RED); 
    rdd.setBackgroundColor(Color.RED); 

    if(currentQ.getANSWER().equals(currentQ.getOPTA())) { 
     rda.setBackgroundColor(Color.GREEN); 
    } else if(currentQ.getANSWER().equals(currentQ.getOPTB())) { 
     rdb.setBackgroundColor(Color.GREEN); 
    } else if(currentQ.getANSWER().equals(currentQ.getOPTC())) { 
     rdc.setBackgroundColor(Color.GREEN); 
    } else if(currentQ.getANSWER().equals(currentQ.getOPTD())) { 
     rdd.setBackgroundColor(Color.GREEN); 
    } 

    setQuestionView(); 

    // WHEN NEXT BUTTON IS CLICKED RETURN TO ACEActivity AND LOAD NEXT QUESTION 
    nextBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(CorrectACEActivity.this, ACEActivity.class); 
      intent.putExtra("nextQuestionID", currentQ + 1); 
      startActivity(intent); 
     } 
    }); 
} 

private void setQuestionView() { 
    txtQuestion.setText(currentQ.getQUESTION()); 
    rda.setText(currentQ.getOPTA()); 
    rdb.setText(currentQ.getOPTB()); 
    rdc.setText(currentQ.getOPTC()); 
    rdd.setText(currentQ.getOPTD()); 
    qid++; 
    } 
} 



第一個問題很完美。但是,在回答第一個問題後,我一回到ACEActivity,我就再次被提出第一個問題。


正如你所看到的,我對此非常陌生,對於任何形式的幫助都會非常高興!謝謝!!

回答

1
intent.putExtra("nextQuestionID", currentQ + 1); 

您在CorrectACEActivity中設置了額外的錯誤,是不是應該像這樣?

intent.putExtra("nextQuestionID", qid+ 1); 
+0

謝謝您的回答!你是對的。我的代碼還有很多其他問題。因此,我會盡力完善我的項目佈局。但非常感謝你指出這一點! – Schwesi

+0

我很高興我的幫助。 請標記爲答案 – Farid

相關問題