2013-02-22 83 views
0

嘿家伙即时通讯开发一个测验,我有50个问题随机存储使用json数组,现在我的问题是所有这些50个问题都显示每次用户将play.is它如果我只想要显示10个问题,可能限制他们... ...我有50个问题存储,因为它是随机选择的,但我只想要10个显示..请帮助我们..谢谢这么多!如何在android中使用json数组限制显示问题

public class Question1<JsonObject> extends Activity { 



Intent menu = null; 
BufferedReader bReader = null; 
static JSONArray quesList = null; 
static int index = 50; 



/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.question10); 

    Thread thread = new Thread() { 
     public void run() { 
      try { 
       Thread.sleep(1 * 1000); 
       finish(); 
       loadQuestions(); 
       Intent intent = new Intent(Question1.this, 
         Question2.class); 
       Question1.this.startActivity(intent); 
      } catch (Exception e) { 
      } 
     } 
    }; 
    thread.start(); 

} 

private void loadQuestions() throws Exception { 
    try { 



     List<JSONObject> 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); 


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

    } 

} 

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; 


} 

回答

0

试图把它这种方式:

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

} 

} 

看起来像你的程序命名混乱。没有特定的代码,但我认为您需要使用List<JSONObject> question而不是quesList

+0

非常感谢@ sandstar..ill试试这个..如果这个会工作的.. :) – user2079544 2013-02-22 11:15:36

+0

其实,我认为它可能更好,甚至解析JSONObject内功能,并有线程完成后像列表不喜欢。 – sandrstar 2013-02-22 11:36:51