0

嘿,伙计们,我正在开发一个测验,其中我使用 json 数组随机存储了 50 个问题,现在我的问题是每次用户玩游戏时都会显示所有这 50 个问题。如果我只想显示 10 个问题,是否可以限制它们?...我存储了 50 个问题,因为它是随机选择的,但我只想显示 10 个。请帮帮我。非常感谢!

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;


}
4

1 回答 1

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.

于 2013-02-22T11:07:58.533 回答