Android Open Source - OpenHSK Quiz Repository






From Project

Back to project page OpenHSK.

License

The source code is released under:

This work is licensed under a Creative Commons Attribution 3.0 Unported License. Original author of word lists: http://lingomi.com/ Original author of definitions: http://cc-cedict.org Original autho...

If you think the Android project OpenHSK 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

package edu.openhsk.repository;
// w w w  . j  a v a2 s  .  c om
import static edu.openhsk.repository.DatabaseMetadata.T_CACHE;
import static edu.openhsk.repository.DatabaseMetadata.T_WORDS;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import edu.openhsk.models.QuizHanzi;

public class QuizRepository {
  private static final String[] QUIZ_WORD_COLUMNS = new String[] {"_id", "word", "pinyin", "definition", "soundfile"};
  
  public List<QuizHanzi> makeQuizList(SQLiteDatabase db, boolean quizIsCached, int wordListId) {
    Cursor cursor = null;
    try {
      String[] selectionArgs = new String[] {String.valueOf(wordListId)};
      if (quizIsCached) {
        cursor = db.query(T_WORDS, QUIZ_WORD_COLUMNS, "wordlistid = ? AND _id IN (SELECT word_id FROM " + T_CACHE + ")", selectionArgs, null, null, null);
      } else {
        cursor = db.query(T_WORDS, QUIZ_WORD_COLUMNS, "wordlistid = ? AND islearned = 0", selectionArgs, null, null, "RANDOM()", "4");
      }
      
      if (cursor.getCount() < 4) { //if word list has >4 non-learned words, widen search criteria
        DatabaseHelper.closeCursor(cursor);
        cursor = db.query(T_WORDS, QUIZ_WORD_COLUMNS, "wordlistid = ?", selectionArgs, null, null, "RANDOM()", "4");
      }
      
      if (cursor.getCount() == 4) {
        List<QuizHanzi> list = new ArrayList<QuizHanzi>(3);
        while (cursor.moveToNext()) {
          list.add(convertDbRowToQuizHanzi(cursor));
        }
        return list;
      } else {
          throw new Exception("Error, wrong amount of words in cursor: " + cursor.getCount());
        }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DatabaseHelper.closeCursor(cursor);
    }
    
    return null;
  }
  
  protected static QuizHanzi convertDbRowToQuizHanzi(Cursor cursor) {
    assert cursor.getColumnCount() == 5;
    int wordId = cursor.getInt(cursor.getColumnIndex("_id"));
    String wordStr = cursor.getString(cursor.getColumnIndex("word"));
    String pinyinStr = cursor.getString(cursor.getColumnIndex("pinyin"));
    String defStr = cursor.getString(cursor.getColumnIndex("definition"));
    String soundStr = cursor.getString(cursor.getColumnIndex("soundfile"));
    return new QuizHanzi(wordId, wordStr, pinyinStr, defStr, soundStr);
  }

  public List<QuizHanzi> makeExamList(SQLiteDatabase db, boolean isCached, int currentWordId, int wordListId) {
    Cursor cursor = null;
    try {
      String[] selectionArgs = {String.valueOf(currentWordId), String.valueOf(wordListId)};
      cursor = db.query(T_WORDS, QUIZ_WORD_COLUMNS, "_id != ? AND wordlistid = ?", selectionArgs , null, null, "RANDOM()", "3");
      if (cursor.getCount() == 3) {
        List<QuizHanzi> list = new ArrayList<QuizHanzi>(3);
        while (cursor.moveToNext()) {
          list.add(convertDbRowToQuizHanzi(cursor));
        }
        return list;
      } else {
        throw new IOException("Error, wrong amount of words in result: " + cursor.getCount());
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      DatabaseHelper.closeCursor(cursor);
    }
    return null;
  }
}




Java Source Code List

edu.openhsk.CharacterListActivity.java
edu.openhsk.CharacterViewActivity.java
edu.openhsk.CreateWordActivity.java
edu.openhsk.CreateWordListActivity.java
edu.openhsk.ExamActivity.java
edu.openhsk.ExamResultActivity.java
edu.openhsk.MainActivity.java
edu.openhsk.QuizActivity.java
edu.openhsk.WordListSelectionActivity.java
edu.openhsk.adapters.WordListViewBinder.java
edu.openhsk.adapters.WordViewBinder.java
edu.openhsk.models.ExamResult.java
edu.openhsk.models.Hanzi.java
edu.openhsk.models.QuizHanzi.java
edu.openhsk.models.WordList.java
edu.openhsk.repository.DatabaseHelper.java
edu.openhsk.repository.DatabaseMetadata.java
edu.openhsk.repository.HanziRepository.java
edu.openhsk.repository.HelpfulCursor.java
edu.openhsk.repository.QuizRepository.java
edu.openhsk.repository.SimpleCursorLoader.java
edu.openhsk.repository.WordListRepository.java
edu.openhsk.service.QuizService.java
edu.openhsk.utils.AsyncSoundPlayer.java
edu.openhsk.utils.CSVExporter.java
edu.openhsk.utils.CSVParser.java
edu.openhsk.utils.PinyinReplacer.java
edu.openhsk.utils.SoundManager.java
edu.openhsk.views.BarGraph.java
edu.openhsk.views.Bar.java
edu.openhsk.views.ExamResultPieChart.java