示例#1
0
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz);

    // Open the database
    dataSource = new QuestionDataSource(this);
    dataSource.open();

    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    viewList = new ArrayList<LinearLayout>(0);

    HealthismQuiz quiz = QuizFactory.build(getResources().openRawResource(R.raw.quiz));

    viewSize = quiz.numQuestions();
    resultList = new ArrayList[viewSize];
    viewTypeArray = new QuestionType[viewSize];
    questionTextArray = new String[viewSize];

    // Load the views for the quiz and populate the database with options in every question
    for (int i = 0; i < viewSize; i++) {
      Question q = quiz.getQuestion(i);
      loadQuiz(q.getType(), q.getText(), q.getOptions().iterator());
      questionTextArray[i] = q.getText();
      viewTypeArray[i] = q.getType();

      // Store all the options for each question into the database
      ListIterator<String> optionsIt = q.getOptions().listIterator();
      while (optionsIt.hasNext()) {
        dataSource.storeOption(i, optionsIt.next());
      }
    }

    // Display the first question of the quiz
    initFirstQuestion();

    // Call the nextButtonOnClick method when the next button is clicked
    Button next = (Button) findViewById(R.id.quiz_nextButton);
    next.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            nextButtonOnClick(v);
          }
        });

    // Call the backButtonOnClick method when the back button is clicked
    Button back = (Button) findViewById(R.id.quiz_backButton);
    back.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            backButtonOnClick(v);
          }
        });
  }
示例#2
0
 @Override
 protected void onResume() {
   dataSource.open();
   super.onResume();
 }
示例#3
0
 @Override
 protected void onPause() {
   dataSource.close();
   super.onPause();
 }
示例#4
0
  /**
   * Change the view after user clicks the next button If all the questions are answered, store the
   * results and return to the main activity
   *
   * @param v
   */
  public void nextButtonOnClick(View v) {
    // Store the result of the current view(question)
    try {
      saveResult();
    } catch (IllegalArgumentException ex) {
      Toast.makeText(getApplicationContext(), "Please pick an answer", 300).show();
      return;
    }

    // Save the results into the database and exit this activity
    if (currentView == viewSize) {
      // Save results into the database
      for (int i = 0; i < viewTypeArray.length; i++) {
        for (int j = 0; j < resultList[i].size(); j++) {
          dataSource.storeAnswer(
              i + 1,
              questionTextArray[i],
              viewTypeArray[i].toString(),
              (Integer) resultList[i].get(j));
        }
      }

      // Set alarm for notification
      setNotifiAlarm();

      // Return to the main activity/screen
      dataSource.close();
      finish();
      Intent intent = new Intent(this, HealthyDroidActivity.class);
      startActivity(intent);
    }

    currentView++;

    if (currentView <= viewList.size()) {
      // Display the current question number
      TextView curQuestText = (TextView) findViewById(R.id.quiz_curQuestNumText);
      curQuestText.setText(currentView + "/" + viewSize);

      // Show the back button
      Button backButton = (Button) findViewById(R.id.quiz_backButton);
      backButton.setVisibility(View.VISIBLE);

      // If the current question is the last question, set the next button
      // to display "Finish"
      if (currentView == viewSize) {
        Button nextButton = (Button) findViewById(R.id.quiz_nextButton);
        nextButton.setText("Finish");
      }
      // Add slide out animation to the previous question
      if (currentView >= 2)
        viewList
            .get(currentView - 2)
            .setAnimation(
                AnimationUtils.loadAnimation(
                    this.getBaseContext(), R.anim.view_transition_out_left));

      lLayout.removeAllViews();
      // Add slide in animation to the current question
      viewList
          .get(currentView - 1)
          .setAnimation(
              AnimationUtils.loadAnimation(this.getBaseContext(), R.anim.view_transition_in_left));
      lLayout.addView(viewList.get(currentView - 1));
      return;
    }
  }