【发布时间】:2016-12-15 17:56:57
【问题描述】:
这是一个从文本文件加载问题的安卓测验应用程序代码 sn-p。 我想在每次下一次点击后随机播放问题和答案,那么我该如何实现随机功能? https://github.com/gitssk/quizfun/blob/master/src/ssk/quizfun/QuizFunActivity.java
https://github.com/gitssk/quizfun/blob/master/res/raw/questions.txt
private void loadQuestions() throws Exception {
try {
InputStream questions = this.getBaseContext().getResources()
.openRawResource(R.raw.questions);
bReader = new BufferedReader(new InputStreamReader(questions));
StringBuilder quesString = new StringBuilder();
String aJsonLine = null;
while ((aJsonLine = bReader.readLine()) != null) {
quesString.append(aJsonLine);
}
Log.d(this.getClass().toString(), quesString.toString());
JSONObject quesObj = new JSONObject(quesString.toString());
quesList = quesObj.getJSONArray("Questions");
Log.d(this.getClass().getName(),
"Num Questions " + quesList.length());
} catch (Exception e){
} finally {
try {
bReader.close();
} catch (Exception e) {
Log.e("", e.getMessage().toString(), e.getCause());
}
}
}
https://github.com/gitssk/quizfun/blob/master/src/ssk/quizfun/QuizFunActivity.java
【问题讨论】:
-
你可以使用 Collections.shuffle 随机播放
-
你能给我举个例子吗
-
Collections.shuffleJSONArray?这甚至可能吗? -
你可以看看Aleadam's solution。
标签: android arrays random arc4random