2014-10-06 55 views
0

我已经提出了包含5个问题的测验应用程序。我做了一个显示测验结果的ResultActivity页面。 我为每个问题添加了20秒的countDown计时器。当倒数计时器结束时,它自动转到下一个问题。当问题完成后,它应该移到ResultActivity页面来显示结果。在测验应用程序中的Android CountDown计时器

我只有一个问题... Afetr达到我的最后一个问题..如果我没有选择任何答案和计时器完成应用程序没有移动到我的ResultActivity页面..计时器一次又一次地开始对同一个问题,直到我选择任何选项

这里是我的代码:

QuizActivity.java

package com.example.triviality; 
import java.util.LinkedHashMap; 
import java.util.List; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.app.Activity; 
import android.content.Intent; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.Toast; 

public class QuizActivity extends Activity { 
    List<question> quesList; 
    public static int score, correct, wrong, wronganswers; 
    public boolean isCorrect; 
    static int qid = 0; 
    int totalCount = 5; 
    Question currentQ; 
    TextView txtQuestion; 
    RadioGroup radioGroup1; 
    RadioButton rda, rdb, rdc; 
    Button butNext; 
    TextView rt; 
    boolean nextFlag = false; 
    boolean isTimerFinished = false; 
    static LinkedHashMap lhm = new LinkedHashMap(); 
    MyCountDownTimer countDownTimer = new MyCountDownTimer(10000 /* 20 Sec */, 
      1000); 
    // final MyCountDownTimer timer = new MyCountDownTimer(20000,1000); 
    public static String[] Correctanswers = new String[5]; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_quiz); 
     DbHelper db = new DbHelper(this); 
     quesList = db.getAllQuestions(); 
     currentQ = quesList.get(qid); 
     txtQuestion = (TextView) findViewById(R.id.textView1); 
     rda = (RadioButton) findViewById(R.id.radio0); 
     rdb = (RadioButton) findViewById(R.id.radio1); 
     rdc = (RadioButton) findViewById(R.id.radio2); 
     butNext = (Button) findViewById(R.id.button1); 
     // radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1); 
     setQuestionView(); 
     // timer.start(); 
     rt = (TextView) findViewById(R.id.rt); 
     rt.setText("20"); 
     countDownTimer.start(); 
     butNext.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       countDownTimer.cancel(); 
       if (getNextQuestion(false)) { 
        // Start The timer again 
        countDownTimer.start(); 
       } 
      } 
     }); 
    } 
    private void setQuestionView() { 
     txtQuestion.setText(currentQ.getQUESTION()); 
     rda.setText(currentQ.getOPTA()); 
     rdb.setText(currentQ.getOPTB()); 
     rdc.setText(currentQ.getOPTC()); 
    } 

    public class MyCountDownTimer extends CountDownTimer { 
     public MyCountDownTimer(long startTime, long interval) { 
      super(startTime, interval); 
     } 

     public void onFinish() { 
      Log.e("Times up", "Times up"); 
      countDownTimer.cancel(); 
      if (getNextQuestion(false)) { 
       // Start The timer again 
       countDownTimer.start(); 
      } 
     } 
     @Override 
     public void onTick(long millisUntilFinished) { 
      rt.setText((millisUntilFinished/1000) + ""); 
      Log.e("Second Gone", "Another Second Gone"); 
      Log.e("Time Remaining", "seconds remaining: " + millisUntilFinished 
        /1000); 
     } 
    } 
    boolean getNextQuestion(boolean c) { 
     nextFlag = true; 
     RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1); 
     // grp.clearCheck(); 
     RadioButton answer = (RadioButton) findViewById(grp 
       .getCheckedRadioButtonId()); 
     if (rda.isChecked() || rdb.isChecked() || rdc.isChecked()) { 
      qid++; 
      Log.d("yourans", currentQ.getANSWER() + " " + answer.getText()); 
      grp.clearCheck(); 
      // wronganswers= 
      if (!c && currentQ.getANSWER().equals(answer.getText())) { 
       correct++; 
      } else { 
       lhm.put(currentQ.getQUESTION(), currentQ.getANSWER()); 
       wrong++; 
      } 

      if (qid < 5) { 
       currentQ = quesList.get(qid); 
       setQuestionView(); 

      } else { 

       score = correct; 
       Intent intent = new Intent(QuizActivity.this, 
         ResultActivity.class); 
       Bundle b = new Bundle(); 
       b.putInt("score", score); // Your score 
       intent.putExtras(b); // Put your score to your next Intent 
       startActivity(intent); 
       return false; 
      } 
     } else { 
      lhm.put(currentQ.getQUESTION(), currentQ.getANSWER()); 
      qid++; 

      if (qid < 5) { 
       currentQ = quesList.get(qid); 
       //currentQ.getANSWER().equals(wrong); 
       wrong++; 
       Log.e("yourans", currentQ.getANSWER()); 
       setQuestionView(); 

      } 
      // wrong++; 
      // Log.e("Without Any Selection ", "without "+wrong); 
      // Toast.makeText(getApplicationContext(), 
      // "Please select atleast one Option",Toast.LENGTH_SHORT).show(); 

     } 

     return true; 
    } 
} 




ResultActivity.java: 

package com.example.triviality; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
public class ResultActivity extends Activity { 
    Button restart; 
    Button check; 

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

     TextView t=(TextView)findViewById(R.id.textResult); 
     TextView t1=(TextView)findViewById(R.id.textResult1); 
     TextView t2=(TextView)findViewById(R.id.textResult2); 
     restart=(Button)findViewById(R.id.restart); 
     check=(Button)findViewById(R.id.check); 

     StringBuffer sb=new StringBuffer(); 
     sb.append("Correct ans: "+QuizActivity.correct+"\n"); 
     StringBuffer sc=new StringBuffer(); 
     sc.append("Wrong ans : "+QuizActivity.wrong+"\n"); 
     StringBuffer sd=new StringBuffer(); 
     sd.append("Final Score : "+QuizActivity.score); 
     t.setText(sb); 
     t1.setText(sc); 
     t2.setText(sd); 
     QuizActivity.correct=0; 
     QuizActivity.wrong=0; 


     check.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       // TODO Auto-generated method stub 
       Intent in=new Intent(getApplicationContext(),CheckActivity.class); 
       startActivity(in); 
      } 
     }); 

restart.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       //QuizActivity quiz=new QuizActivity(); 

       // TODO Auto-generated method stub 
       Intent intent=new Intent(getApplicationContext(),QuizActivity.class); 

       QuizActivity.lhm.clear(); 
       QuizActivity.qid=0; 
       startActivity(intent); 
       //quiz.countDownTimer.onTick(19); 
      } 
     }); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_result, menu); 
     return true; 
    } 
} 

回答

0

这是因为如果用户做出了n您的回报虚假陈述将永远不会达到o选择:

if (rda.isChecked() || rdb.isChecked() || rdc.isChecked()){} 

在这个if else语句中您将在稍后返回false。但是如果没有选中,你会自动返回true,因为上面这个语句中的代码块不会被破坏。然后在您的计时器你说:如果用户没有进行选择,所以计时器一次又一次地开始

if (getNextQuestion(false)) { 
      // Start The timer again 
      countDownTimer.start(); 
     } 

getNextQuestion是百达真。

+0

那么这将是什么解决方案?你可以帮我解决它... :( – vvv12 2014-10-06 14:22:58

+0

我会试试看。你的意图后面返回一个值?你是什么意图给予一个布尔值作为参数? – Opiatefuchs 2014-10-07 06:55:57

+0

你可以给我替代解决方案..什么可以我用,而不是布尔值... – vvv12 2014-10-07 10:19:27