简体   繁体   English

如何在Android中使用json数组限制问题的显示

[英]how to limit the display of questions using json array in android

hey guys im developing a quiz where i have 50 questions randomly stored using json array, now my problem is all those 50 questions are displayed everytime the user will play.is it possible to limit them if iwant only 10 questions to display?...i have 50 questions stored because it is randomly selected but i want only 10 to display..please help me guys..thanks so much! 大家好,我正在开发一个测验,其中我使用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;


}

Try to put it this way: 尝试这样说:

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

}

}

Looks like naming in Your program is messed up. 看起来您的程序中的命名混乱了。 There's no specific code, but I assume that You need to use List<JSONObject> question instead of quesList . 没有特定的代码,但是我假设您需要使用List<JSONObject> question而不是quesList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM