use of com.google.samples.apps.topeka.model.quiz.Quiz in project android-topeka by googlesamples.
the class ScoreAdapter method getView.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = createView(parent);
}
final Quiz quiz = getItem(position);
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
viewHolder.mQuizView.setText(quiz.getQuestion());
viewHolder.mAnswerView.setText(quiz.getStringAnswer());
setSolvedStateForQuiz(viewHolder.mSolvedState, position);
return convertView;
}
use of com.google.samples.apps.topeka.model.quiz.Quiz in project android-topeka by googlesamples.
the class TopekaDatabaseHelper method updateQuizzes.
/**
* Updates a list of given quizzes.
*
* @param writableDatabase The database to write the quizzes to.
* @param quizzes The quizzes to write.
*/
private static void updateQuizzes(SQLiteDatabase writableDatabase, List<Quiz> quizzes) {
Quiz quiz;
ContentValues quizValues = new ContentValues();
String[] quizArgs = new String[1];
for (int i = 0; i < quizzes.size(); i++) {
quiz = quizzes.get(i);
quizValues.clear();
quizValues.put(QuizTable.COLUMN_SOLVED, quiz.isSolved());
quizArgs[0] = quiz.getQuestion();
writableDatabase.update(QuizTable.NAME, quizValues, QuizTable.COLUMN_QUESTION + "=?", quizArgs);
}
}
use of com.google.samples.apps.topeka.model.quiz.Quiz in project android-topeka by googlesamples.
the class TopekaDatabaseHelper method getQuizzes.
/**
* Creates objects for quizzes according to a category id.
*
* @param categoryId The category to create quizzes for.
* @param database The database containing the quizzes.
* @return The found quizzes or an empty list if none were available.
*/
private static List<Quiz> getQuizzes(final String categoryId, SQLiteDatabase database) {
final List<Quiz> quizzes = new ArrayList<>();
final Cursor cursor = database.query(QuizTable.NAME, QuizTable.PROJECTION, QuizTable.FK_CATEGORY + " LIKE ?", new String[] { categoryId }, null, null, null);
cursor.moveToFirst();
do {
quizzes.add(createQuizDueToType(cursor));
} while (cursor.moveToNext());
cursor.close();
return quizzes;
}
use of com.google.samples.apps.topeka.model.quiz.Quiz in project android-topeka by googlesamples.
the class TopekaDatabaseHelper method getCategory.
/**
* Gets a category from the given position of the cursor provided.
*
* @param cursor The Cursor containing the data.
* @param readableDatabase The database that contains the quizzes.
* @return The found category.
*/
private static Category getCategory(Cursor cursor, SQLiteDatabase readableDatabase) {
// "magic numbers" based on CategoryTable#PROJECTION
final String id = cursor.getString(0);
final String name = cursor.getString(1);
final String themeName = cursor.getString(2);
final Theme theme = Theme.valueOf(themeName);
final String isSolved = cursor.getString(3);
final boolean solved = getBooleanFromDatabase(isSolved);
final int[] scores = JsonHelper.jsonArrayToIntArray(cursor.getString(4));
final List<Quiz> quizzes = getQuizzes(id, readableDatabase);
return new Category(name, id, theme, quizzes, scores, solved);
}
use of com.google.samples.apps.topeka.model.quiz.Quiz in project android-topeka by googlesamples.
the class TopekaDatabaseHelper method updateCategory.
/**
* Updates values for a category.
*
* @param context The context this is running in.
* @param category The category to update.
*/
public static void updateCategory(Context context, Category category) {
if (mCategories != null && mCategories.contains(category)) {
final int location = mCategories.indexOf(category);
mCategories.remove(location);
mCategories.add(location, category);
}
SQLiteDatabase writableDatabase = getWritableDatabase(context);
ContentValues categoryValues = createContentValuesFor(category);
writableDatabase.update(CategoryTable.NAME, categoryValues, CategoryTable.COLUMN_ID + "=?", new String[] { category.getId() });
final List<Quiz> quizzes = category.getQuizzes();
updateQuizzes(writableDatabase, quizzes);
}
Aggregations