Android Open Source - AndroidSqliteMultichoiceExample Questions Activity






From Project

Back to project page AndroidSqliteMultichoiceExample.

License

The source code is released under:

Apache License

If you think the Android project AndroidSqliteMultichoiceExample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright 2014 Yehezkel (Zack) Yovel//  w ww  .ja v  a 2 s . c  o  m
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package zack.examples.questiondb;

import java.util.List;

import zack.examples.questiondb.database.DataAccessObject;
import zack.examples.qustiondb.entities.Question;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class QuestionsActivity extends Activity implements OnItemClickListener {

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

    Bundle extra = getIntent().getExtras();
    if (extra != null) {
      PopulateQuestionsListTask questionsTask = new PopulateQuestionsListTask();
      questionsTask.execute(extra.getString(MainActivity.EXTRA_SUBJECT));
    } else {
      throw new RuntimeException("Must send a subject with the key "
          + MainActivity.EXTRA_SUBJECT
          + " with the intent for QuestionsActivity.");
    }
  }

  private class PopulateQuestionsListTask extends
      AsyncTask<String, Void, List<Question>> {

    @Override
    protected List<Question> doInBackground(String... params) {
      List<Question> result = null;
      String subject = params[0];
      if (subject != null) {
        DataAccessObject dao = DataAccessObject
            .getInstance(QuestionsActivity.this);
        result = dao.getQuestionsForSubject(subject);
      }
      return result;
    }

    @Override
    protected void onPostExecute(List<Question> result) {
      ListView listView = (ListView) findViewById(R.id.listView1);
      ListAdapter adapter = new ArrayAdapter<Question>(
          QuestionsActivity.this,
          android.R.layout.simple_list_item_1, result);
      listView.setAdapter(adapter);
      listView.setOnItemClickListener(QuestionsActivity.this);
    }

  }

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
      long id) {
    Question question = (Question) parent.getAdapter().getItem(position);
    showQuestionDialog(question);
  }

  private void showQuestionDialog(Question question) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final String correct = question.answers.get(0);
    final CharSequence[] items = new CharSequence[question.answers.size() - 1];
    for (int i = question.answers.size() - 1; i > 0; i--) {
      items[i - 1] = question.answers.get(i);
    }
    OnClickListener listener = new OnClickListener() {

      @Override
      public void onClick(DialogInterface dialog, int which) {
        if (items[which].equals(correct)) {
          Toast.makeText(QuestionsActivity.this, "Correct answer!",
              Toast.LENGTH_LONG).show();
        } else {
          Toast.makeText(QuestionsActivity.this, "Wrong answer...",
              Toast.LENGTH_LONG).show();
        }
      }
    };
    builder.setItems(items, listener);
    builder.create().show();
  }
}




Java Source Code List

zack.examples.questiondb.MainActivity.java
zack.examples.questiondb.QuestionsActivity.java
zack.examples.questiondb.database.DBContract.java
zack.examples.questiondb.database.DataAccessObject.java
zack.examples.questiondb.database.QuestionsOpenHelper.java
zack.examples.qustiondb.entities.Question.java