如何限制使用Android中的JSON数组中的问答题的显示? [英] How to limit the display of questions in the quiz using json array in android?

查看:169
本文介绍了如何限制使用Android中的JSON数组中的问答题的显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个测验,我已经存储了50个问题,这些问题是使用JSON数组访问,我想只显示10个问题,我的问题是,所有这些50个问题都显示每次用户将play.Is它可能限制他们,如果我想只有10个问题,以显示?我的第10问题后比分类表现。

你们能帮帮我?

 列表<&JSONObject的GT;问题= NULL;    私人无效loadQuestions()抛出异常{
        尝试{
            问题的InputStream = this.getBaseContext()。getResources()
                    .openRawResource(R.raw.questions);
            面包屑=新的BufferedReader(新的InputStreamReader(问题));
            StringBuilder的quesString =新的StringBuilder();
            字符串aJsonLine = NULL;
            而((aJsonLine = bReader.readLine())!= NULL){
                quesString.append(aJsonLine);
            }            Log.d(this.getClass()的toString(),quesString.toString());
            JSONObject的quesObj =新的JSONObject(quesString.toString());
            quesList = quesObj.getJSONArray(问题);
            Log.d(this.getClass()。的getName(),
                    数问题+ quesList.length());              问题=新的ArrayList<&JSONObject的GT;();
                 INT N = Math.min(10,quesList.length());
                 的for(int i = 0; I< N;我++){
                     JSONObject的questions1 = quesList.getJSONObject(I)
                     question.add(questions1);
                 }
        }赶上(例外五){        } {最后
            尝试{
                bReader.close();
            }赶上(例外五){
                Log.e(,e.getMessage()的toString(),e.​​getCause());
            }        }    }    公共静态JSONArray getQuesList()抛出JSONException {          随机RND =新的随机();            对于(INT I = quesList.length() - 1; I> = 0;我 - )
            {
              诠释J = rnd.nextInt第(i + 1);
              //简单交换
              Object对象= quesList.get(J);
              quesList.put(J,quesList.get(ⅰ));
              quesList.put(ⅰ,对象);
            }
            返回quesList;
    }
}
    @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.startquiz);    尝试{
        得分= getIntent()getIntExtra(得分,0);
        项目=(的TextView)findViewById(R.id.displayitems);
        问题=(的TextView)findViewById(R.id.displayquestion);
        ANSWER1 =(单选)findViewById(R.id.option1);
        ANSWER2 =(单选)findViewById(R.id.option2);
        ANSWER3 =(单选)findViewById(R.id.option3);
        答案=(RadioGroup中)findViewById(R.id.QueGroup1);
        接下来=(按钮)findViewById(R.id.selected);
        next.setOnClickListener(nextListener);        选择=新的INT [Question1.getQuesList()长()];
        java.util.Arrays.fill(选择,-1);
        correctAns =新INT [Question1.getQuesList()长()];
        java.util.Arrays.fill(correctAns,-1);        this.showQuestion(0,审查);    }赶上(例外五){
        Log.e(,e.getMessage()的toString(),e.​​getCause());
    }}私人无效showQuestion(INT qIndex,布尔评审){
    尝试{
        JSONObject的aQues = Question1.getQuesList()。getJSONObject(
                qIndex);
        字符串quesValue = aQues.getString(问题);
        如果(correctAns [qIndex] == -1){
            串correctAnsStr = aQues.getString(CorrectAnswer);
            correctAns [qIndex] =的Integer.parseInt(co​​rrectAnsStr);
        }        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(答案);
        字符串AANS = ansList.getJSONObject(0).getString(答);
        answer1.setText(aAns.toCharArray(),0,aAns.length());
        AANS = ansList.getJSONObject(1).getString(答);
        answer2.setText(aAns.toCharArray(),0,aAns.length());
        AANS = ansList.getJSONObject(2).getString(答);
        answer3.setText(aAns.toCharArray(),0,aAns.length());
        Log.d(,选择[qIndex] +);
        如果(选择了[qIndex] == 0)
            answers.check(R.id.option1);
        如果(选择了[qIndex] == 1)
            answers.check(R.id.option2);
        如果(选择了[qIndex] == 2)
            answers.check(R.id.option3);        的setText();
        如果(quesIndex ==(Question1.getQuesList()长() - 1))
            next.setEnabled(假);        如果(quesIndex<(Question1.getQuesList()长() - 1))
            next.setEnabled(真);        如果(综述){
            Log.d(检讨,选择[qIndex] ++ correctAns [qIndex]);
            ;
            如果(选择了[qIndex]!= correctAns [qIndex]){
                如果(选择了[qIndex] == 0)
                    answer1.setTextColor(Color.RED);
                如果(选择了[qIndex] == 1)
                    answer2.setTextColor(Color.RED);
                如果(选择了[qIndex] == 2)
                    answer3.setTextColor(Color.RED);
            }
            如果(correctAns [qIndex] == 0)
                answer1.setTextColor(Color.GREEN);
            如果(correctAns [qIndex] == 1)
                answer2.setTextColor(Color.GREEN);
            如果(correctAns [qIndex] == 2)
                answer3.setTextColor(Color.GREEN);
        }
    }赶上(例外五){
        Log.e(this.getClass()的toString(),e.​​getMessage(),e.​​getCause());
    }
}
私人无效setAnswer(){
    如果(answer1.isChecked())
        选择了[quesIndex] = 0;
    如果(answer2.isChecked())
        选择[quesIndex] = 1;
    如果(answer3.isChecked())
        选择[quesIndex] = 2;    Log.d(,Arrays.toString(选择));
    Log.d(,Arrays.toString(correctAns));}私人OnClickListener nextListener =新OnClickListener(){
    公共无效的onClick(视图v){
        INT I = correctAns.length;        如果(V ==答案)​​{
            如果(correctAns [I] ==选择[I])
                    {                        得分++;
                        Toast.makeText(getApplicationContext(),你的答案是正确的,Toast.LENGTH_SHORT).show();
                    }其他
                    {
                    Toast.makeText(getApplicationContext(),你的答案是错的......+ correctAns,Toast.LENGTH_SHORT).show();
                    }
        }
        quesIndex ++;
        尝试{
            如果(quesIndex方式> = Question1.getQuesList()长度())
                quesIndex = Question1.getQuesList()长() - 1。
        }赶上(JSONException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }        showQuestion(quesIndex,审查);    }
};
私人无效的setText()抛出JSONException {
    this.setTitle(问题+(quesIndex + 1)+出
            + Question1.getQuesList()长());
    items.setGravity(250);
}公共无效重装(){
    setAnswer();意向意图= getIntent();
overridePendingTransition(0,0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
完();overridePendingTransition(0,0);
startActivity(意向);
}


解决方案

从我的理解,它可能会更好,如果你下载整个JSON响应,仅阅读相关部分。

在理想情况下,如果你想有10个问题,并有共50这意味着你会想有10出现5次。为了做到这一点,你可能只是这一切存储在一个ArrayList,然后当你到下一组只是抵消你到了10的位置,然后抓住这些元素并显示它们,直到你出的重复此过程的问题。或另一种方法是在每次使用后取下顶部集,只是继续阅读前10名各一次。

请记住,你抢的新问题您必须与你正在阅读的新元素更新内容ArrayList中。

Hey guys I'm developing a quiz where I have 50 questions stored which are accessed using json array and I want to display only 10 questions, my problem is that all those 50 questions are displayed everytime the user will play.Is it possible to limit them if I want only 10 questions to display? After my 10th question the score class is showed.

Can you guys please help me with this?

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

解决方案

From my understand it would probably be better if you download the entire json response and read only the relevant pieces.

Ideally if you want to have 10 questions and have a total of 50 that means you are going to want to have the 10 occur 5 times. In order to do this, you could just store it all in an ArrayList and then as you get to the next set just offset the position you go to by 10 and then grab those elements and display them and repeat this process until you are out of questions. Or another way would be to remove the top set after each use and just keep reading the top 10 each time.

Keep in mind that as you grab new questions you MUST to update the content with the new elements you are reading the the ArrayList.

这篇关于如何限制使用Android中的JSON数组中的问答题的显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆