2013-02-22 78 views
0

嘿,我正在开发一个测验,我有50个问题存储,使用json数组访问,我只想显示10个问题,我的问题是所有这50个问题都会在用户每次播放时显示。限制他们,如果我只想要显示10个问题?在我的第10个问题后,显示了评分等级。如何在android中使用json数组来限制问题的显示?

你们能帮我解决吗?

List<JSONObject> question = null; 

    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()); 

       question = new ArrayList<JSONObject>(); 
       int n = Math.min(10, quesList.length()); 
       for(int i = 0; i < n; i++) { 
        JSONObject questions1 = quesList.getJSONObject(i); 
        question.add(questions1); 


       } 
     } catch (Exception e) { 

     } finally { 
      try { 
       bReader.close(); 
      } catch (Exception e) { 
       Log.e("", e.getMessage().toString(), e.getCause()); 
      } 

     } 

    } 

    public static JSONArray getQuesList()throws JSONException{ 

      Random rnd = new Random(); 

      for (int i = quesList.length() - 1; i >= 0; i--) 
      { 
       int j = rnd.nextInt(i + 1); 
       // Simple swap 
       Object object = quesList.get(j); 
       quesList.put(j, quesList.get(i)); 
       quesList.put(i, object); 
      } 
      return quesList; 


    } 
} 


    @Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.startquiz); 

    try { 
     score = getIntent().getIntExtra("score",0); 
     items = (TextView)findViewById(R.id.displayitems); 
     question = (TextView) findViewById(R.id.displayquestion); 
     answer1 = (RadioButton) findViewById(R.id.option1); 
     answer2 = (RadioButton) findViewById(R.id.option2); 
     answer3 = (RadioButton) findViewById(R.id.option3); 
     answers = (RadioGroup) findViewById(R.id.QueGroup1); 


     next = (Button) findViewById(R.id.selected); 
     next.setOnClickListener(nextListener); 

     selected = new int[Question1.getQuesList().length()]; 
     java.util.Arrays.fill(selected, -1); 
     correctAns = new int[Question1.getQuesList().length()]; 
     java.util.Arrays.fill(correctAns, -1); 

     this.showQuestion(0, review); 



    } catch (Exception e) { 
     Log.e("", e.getMessage().toString(), e.getCause()); 
    } 

} 

private void showQuestion(int qIndex, boolean review) { 
    try { 
     JSONObject aQues = Question1.getQuesList().getJSONObject(
       qIndex); 
     String quesValue = aQues.getString("Question"); 
     if (correctAns[qIndex] == -1) { 
      String correctAnsStr = aQues.getString("CorrectAnswer"); 
      correctAns[qIndex] = Integer.parseInt(correctAnsStr); 
     } 

     question.setText(quesValue.toCharArray(), 0, quesValue.length()); 
     answers.check(-1); 
     answer1.setTextColor(Color.BLACK); 
     answer2.setTextColor(Color.BLACK); 
     answer3.setTextColor(Color.BLACK); 
     JSONArray ansList = aQues.getJSONArray("Answers"); 
     String aAns = ansList.getJSONObject(0).getString("Answer"); 
     answer1.setText(aAns.toCharArray(), 0, aAns.length()); 
     aAns = ansList.getJSONObject(1).getString("Answer"); 
     answer2.setText(aAns.toCharArray(), 0, aAns.length()); 
     aAns = ansList.getJSONObject(2).getString("Answer"); 
     answer3.setText(aAns.toCharArray(), 0, aAns.length()); 
     Log.d("", selected[qIndex] + ""); 
     if (selected[qIndex] == 0) 
      answers.check(R.id.option1); 
     if (selected[qIndex] == 1) 
      answers.check(R.id.option2); 
     if (selected[qIndex] == 2) 
      answers.check(R.id.option3); 

     setText(); 
     if (quesIndex == (Question1.getQuesList().length() - 1)) 
      next.setEnabled(false); 

     if (quesIndex < (Question1.getQuesList().length() - 1)) 
      next.setEnabled(true); 

     if (review) { 
      Log.d("review", selected[qIndex] + "" + correctAns[qIndex]); 
      ; 
      if (selected[qIndex] != correctAns[qIndex]) { 
       if (selected[qIndex] == 0) 
        answer1.setTextColor(Color.RED); 
       if (selected[qIndex] == 1) 
        answer2.setTextColor(Color.RED); 
       if (selected[qIndex] == 2) 
        answer3.setTextColor(Color.RED); 
      } 
      if (correctAns[qIndex] == 0) 
       answer1.setTextColor(Color.GREEN); 
      if (correctAns[qIndex] == 1) 
       answer2.setTextColor(Color.GREEN); 
      if (correctAns[qIndex] == 2) 
       answer3.setTextColor(Color.GREEN); 
     } 
    } catch (Exception e) { 
     Log.e(this.getClass().toString(), e.getMessage(), e.getCause()); 
    } 
} 


private void setAnswer() { 
    if (answer1.isChecked()) 
     selected[quesIndex] = 0; 
    if (answer2.isChecked()) 
     selected[quesIndex] = 1; 
    if (answer3.isChecked()) 
     selected[quesIndex] = 2; 



    Log.d("", Arrays.toString(selected)); 
    Log.d("", Arrays.toString(correctAns)); 

} 

private OnClickListener nextListener = new OnClickListener() { 
    public void onClick(View v) { 
     int i = correctAns.length; 

     if (v == answers){ 
      if (correctAns[i] == selected[i]) 
        { 

         score++; 
         Toast.makeText(getApplicationContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show(); 
        }else 
        { 
        Toast.makeText(getApplicationContext(), "Your answer is wrong..." + correctAns, Toast.LENGTH_SHORT).show(); 
        } 
     } 


     quesIndex++; 
     try { 
      if (quesIndex >= Question1.getQuesList().length()) 
       quesIndex = Question1.getQuesList().length() - 1; 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     showQuestion(quesIndex, review); 

    } 
}; 
private void setText() throws JSONException { 
    this.setTitle("Question " + (quesIndex + 1) + " out of " 
      + Question1.getQuesList().length()); 
    items.setGravity(250); 
} 

public void reload() { 
    setAnswer(); 



Intent intent = getIntent(); 
overridePendingTransition(0, 0); 
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
finish(); 

overridePendingTransition(0, 0); 
startActivity(intent); 
} 
+0

为什么你在主线程中睡了1秒? – JoxTraex 2013-02-22 13:31:00

+0

这是需要扫描的很多代码。如果可以的话,只需发布​​相关部分。 – 2013-02-22 13:34:30

+0

@JoxTraex ok..let我更新我的代码... – user2079544 2013-02-22 13:38:06

回答

0

从我的理解,如果你下载整个JSON响应和只读相关的片它可能会更好。

理想的情况下,如果你想有10个问题,总共有50个,这意味着你想要10次出现5次。为了做到这一点,你可以将它全部存储在一个ArrayList中,然后当你到达下一个集合时,将你到达的位置偏移10,然后抓住这些元素并显示它们并重复这个过程,直到你离开的问题。或者,另一种方法是在每次使用后删除顶部集合,并且每次都继续阅读前10名。

请记住,当您抓住新问题时必须用您正在阅读ArrayList的新元素更新内容。

+0

plesae向我展示示例代码或只是引用我到另一个链接,我是relted我的问题。谢谢 – user2079544 2013-02-22 22:50:45

相关问题