所以我有一个问题对象
public class Question {
private int id;
private String question;
private String picture;
private String option1;
private String option2;
private String option3;
private String option4;
private String correctAnswer;
}
我使用JSON格式将数据解析到MySQL数据库中,如下所示
private class AsyncJsonObject extends AsyncTask<String, Void, String> {
private ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httpPost = new HttpPost("url");
String jsonResult = "";
try {
HttpResponse response = httpClient.execute(httpPost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
System.out.println("Returned Json object " + jsonResult.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonResult;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = ProgressDialog.show(HistoryActivity.this, "Downloading Questions","Please Wait....", true);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
System.out.println("Resulted Value: " + result);
parsedObject = returnParsedJsonObject(result);
if(parsedObject == null){
return;
}
quizCount = parsedObject.size();
firstQuestion = parsedObject.get(0);
mQuestionView.setText(firstQuestion.getQuestion());
Picasso.with(getApplicationContext())
.load(firstQuestion.getPicture())
// .placeholder(R.drawable.placeholder)
// .resize(imgWidth, imgHeight)
// .centerCrop()
.into(mImageView);
mButtonChoice1.setText(firstQuestion.getOption1());
mButtonChoice2.setText(firstQuestion.getOption2());
mButtonChoice3.setText(firstQuestion.getOption3());
mButtonChoice4.setText(firstQuestion.getOption4());
mAnswer = firstQuestion.getCorrectAnswer();
mQuestionNumber++;
num.setText("Question " + mQuestionNumber);
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = br.readLine()) != null) {
answer.append(rLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return answer;
}
}
private List<Question> returnParsedJsonObject(String result){
List<Question> jsonObject = new ArrayList<Question>();
JSONObject resultObject = null;
JSONArray jsonArray = null;
Question newItemObject = null;
try {
resultObject = new JSONObject(result);
System.out.println("Testing the water " + resultObject.toString());
jsonArray = resultObject.optJSONArray("questions");
} catch (JSONException e) {
e.printStackTrace();
}
if (jsonArray != null) { // check jsonArray is null?
for (int i = 0; i < 10; i++) {
JSONObject jsonChildNode = null;
try {
jsonChildNode = jsonArray.getJSONObject(i);
int id = jsonChildNode.getInt("id");
String question = jsonChildNode.getString("question");
String picture = jsonChildNode.getString("picture");
String answerOption1 = jsonChildNode.getString("option1");
String answerOption2 = jsonChildNode.getString("option2");
String answerOption3 = jsonChildNode.getString("option3");
String answerOption4 = jsonChildNode.getString("option4");
String correctAnswer = jsonChildNode.getString("correct");
newItemObject = new Question(id, question, picture, answerOption1, answerOption2, answerOption3, answerOption4, correctAnswer);
jsonObject.add(newItemObject);
}
catch (JSONException e) {
e.printStackTrace();
}
}
return jsonObject;
}
return jsonObject;
}
private void showNextQuestion() {
firstQuestion = parsedObject.get(currentQuizQuestion);
mQuestionView.setText(firstQuestion.getQuestion());
Picasso.with(getApplicationContext())
.load(firstQuestion.getPicture())
// .placeholder(R.drawable.placeholder)
// .resize(imgWidth, imgHeight)
// .centerCrop()
.into(mImageView);
mButtonChoice1.setText(firstQuestion.getOption1());
mButtonChoice2.setText(firstQuestion.getOption2());
mButtonChoice3.setText(firstQuestion.getOption3());
mButtonChoice4.setText(firstQuestion.getOption4());
mAnswer = firstQuestion.getCorrectAnswer();
mQuestionNumber++;
num.setText("Question " + mQuestionNumber);
}
从这里可以看到,每个用户游戏会话我只能检索10个问题
for (int i = 0; i < 10; i++) {
我正在努力实现三件事
请问什么是最好的方法?
我还读到我可以使用SELECT * FROM TABLE ORDER BY RANDOM() LIMIT 10
直接从MySQL随机化数据,但是对于大数据也不是最佳实践,因为它会减慢响应速度,那么我该如何在android中做到这一点?
谢谢
答案 0 :(得分:1)
1)检索列表中的10个问题后,您可以随机排列此列表以随机分配10个问题的位置
2)与此相同。您可以首先将所有选项添加到字符串列表中。然后随机播放此列表。然后,将此混洗的列表分配给选择,以确保始终遵循不同的顺序。
以上两个的代码如下。我还没有测试。可能需要进行一些调试,但是从思想上来说,它应该可以工作:
private List<Question> returnParsedJsonObject(String result){
List<Question> jsonObject = new ArrayList<Question>();
JSONObject resultObject = null;
JSONArray jsonArray = null;
Question newItemObject = null;
try {
resultObject = new JSONObject(result);
System.out.println("Testing the water " + resultObject.toString());
jsonArray = resultObject.optJSONArray("questions");
} catch (JSONException e) {
e.printStackTrace();
}
if (jsonArray != null) { // check jsonArray is null?
for (int i = 0; i < 10; i++) {
JSONObject jsonChildNode = null;
try {
jsonChildNode = jsonArray.getJSONObject(i);
int id = jsonChildNode.getInt("id");
String question = jsonChildNode.getString("question");
String picture = jsonChildNode.getString("picture");
// The below code will shuffle the choices
List<String> choices = new ArrayList<String>();
choices.add(jsonChildNode.getString("option1"));
choices.add(jsonChildNode.getString("option2"));
choices.add(jsonChildNode.getString("option3"));
choices.add(jsonChildNode.getString("option4"));
Collections.shuffle(choices);
String answerOption1 = choices.get(0);
String answerOption2 = choices.get(1);
String answerOption3 = choices.get(2);
String answerOption4 = choices.get(3);
String correctAnswer = jsonChildNode.getString("correct");
newItemObject = new Question(id, question, picture, answerOption1, answerOption2, answerOption3, answerOption4, correctAnswer);
jsonObject.add(newItemObject);
}
catch (JSONException e) {
e.printStackTrace();
}
}
Collections.shuffle(jsonObject); //This will randomize the 10 questions
return jsonObject;
}
return jsonObject;
}
3)对于第三个问题,由于您说您有9个类别,因此可以执行以下操作: 在9个表的每个表中,添加一列“ viewedusers”。此列基本上将存储已经查看过此特定问题的用户的ID(以逗号分隔)(每次用户查看问题时,您都将该用户ID附加到该特定类别的列中)。
现在,每次填充10个问题之前,您首先要检查用户ID是否已经存在,如果是,则不包括该问题。如果最后您不能回答10个问题,则可以显示“出问题”之类的消息。