Режим экзамена в тестирование Android Studio
Переделываю приложение тестирование, нужно создать режим Экзамена. 10 случайных вопросов из всех. База вопросов из Json файла. Помогите реализовать, я новичок в этом деле, как не пробовал не получается. Уже бьюсь неделю
private void loadJson() {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
//todo: заменить QUESTION_FILE
br = new BufferedReader(new InputStreamReader(getAssets().open(AppConstant.QUESTION_FILEEXAM)));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
parseJson(sb.toString());
}
public void parseJson(String jsonData) {
try {
JSONObject jsonObjMain = new JSONObject(jsonData);
JSONArray jsonArray = jsonObjMain.getJSONArray(AppConstant.JSON_KEY_QUESTIONNAIRY);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
String question = jsonObj.getString(AppConstant.JSON_KEY_QUESTION);
int correctAnswer = Integer.parseInt(jsonObj.getString(AppConstant.JSON_KEY_CORRECT_ANS));
JSONArray jsonArray2 = jsonObj.getJSONArray(AppConstant.JSON_KEY_ANSWERS);
ArrayList<String> contents = new ArrayList<>();
ArrayList<String> backgroundColors = new ArrayList<>();
for (int j = 0; j < jsonArray2.length(); j++) {
String item_title = jsonArray2.get(j).toString();
contents.add(item_title);
backgroundColors.add(AppConstant.COLOR_WHITE);
}
mItemList.add(new QuizModel(question, contents, correctAnswer, backgroundColors));
Collections.shuffle(mItemList);
}
mQuestionsCount = mItemList.size();
Collections.shuffle(mItemList);
hideLoader();
updateQuestionsAndAnswers();
} catch (JSONException e) {
e.printStackTrace();
showEmptyView();
}
}
Ответы (1 шт):
Автор решения: Андрей Товтин
→ Ссылка
Не взирая на все плохие комментарии, вот мой ответ (как смог). Учитывая что у меня 244 вопроса
public void setNextQuestion() {
if (isSoundOn) {
mBeatBox.play(mSounds.get(AppConstant.BUNDLE_KEY_FIRST_INDEX));
}
mUserHasPressed = false;
if (mQuestionPosition < mItemList.size() - 235 && mLifeCounter > 0) {
mQuestionPosition++;
if (mQuestionPosition % AppConstant.ADS_INTERVAL == 0) {
}
updateQuestionsAndAnswers();
} else if (mQuestionPosition < mItemList.size() - 235 && mLifeCounter == 0 && mRewardedVideoAd.isLoaded()) {
FragmentManager manager = getSupportFragmentManager();
DialogUtilities dialog = DialogUtilities.newInstance(getString(R.string.reward_dialog_title), getString(R.string.reward_dialog_message), getString(R.string.yes), getString(R.string.no), AppConstant.BUNDLE_KEY_REWARD_OPTION);
dialog.show(manager, AppConstant.BUNDLE_KEY_DIALOG_FRAGMENT);
} else {
ActivityUtilities.getInstance().invokeScoreCardActivity(mActivity, ScoreCardActivity.class, mScore, mWrongAns, mSkip, mResultList, true);
}
}