TAGS :Viewed: 1 - Published at: a few seconds ago

[ How to configure 'Next' button ]

I intend to make a quiz app. But I could not exactly configure the next question button. I tried to make a promptTexts method like below.

public int promptTexts(List<String> optionAs, List<String> optionBs,
        List<String> optionCs, List<String> optionDs,
        List<String> questions, int i) {

        i++;
        txt_questionAdjective.setText(questions.get(i));
        ((RadioButton) grp_options.getChildAt(0)).setText(optionAs.get(i));
        ((RadioButton) grp_options.getChildAt(1)).setText(optionBs.get(i));
        ((RadioButton) grp_options.getChildAt(2)).setText(optionCs.get(i));
        ((RadioButton) grp_options.getChildAt(3)).setText(optionDs.get(i));
        return i;
}

And my onCreate method is like below.

Button btn_nextQuestion;
    RadioGroup grp_options;
    TextView txt_questionAdjective;
    TextView txt_optionA;
    TextView txt_optionB;
    TextView txt_optionC;
    TextView txt_optionD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz_adjectives);
        // Hangi xml dosyasının dikkate alınacağı belirlendi.

        final List<String> questions = new ArrayList();
        final List<String> optionAs = new ArrayList();
        final List<String> optionBs = new ArrayList();
        final List<String> optionCs = new ArrayList();
        final List<String> optionDs = new ArrayList();

        // Reading json file from assets folder
        StringBuffer sb = new StringBuffer();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(getAssets().open(
                    "Questions.json")));
            String temp;
            while ((temp = br.readLine()) != null)
                sb.append(temp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close(); // stop reading
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String myjsonstring = sb.toString();
        // Try to parse JSON
        try {
            // Creating JSONObject from String
            JSONObject jsonObjMain = new JSONObject(myjsonstring);

            // Creating JSONArray from JSONObject
            JSONArray jsonArray = jsonObjMain.getJSONArray("questions");

            // JSONArray has x JSONObject
            for (int i = 0; i < jsonArray.length(); i++) {

                // Creating JSONObject from JSONArray
                JSONObject jsonObj = jsonArray.getJSONObject(i);

                // Getting data from individual JSONObject
                int id = jsonObj.getInt("id");
                String question = jsonObj.getString("question");
                String optionA = jsonObj.getString("optionA");
                String optionB = jsonObj.getString("optionB");
                String optionC = jsonObj.getString("optionC");
                String optionD = jsonObj.getString("optionD");
                String rightAnswer = jsonObj.getString("rightAnswer");

                questions.add(jsonObj.getString("question"));
                optionAs.add(jsonObj.getString("optionA"));
                optionBs.add(jsonObj.getString("optionB"));
                optionCs.add(jsonObj.getString("optionC"));
                optionDs.add(jsonObj.getString("optionD"));

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        // The question is being prompted.
        txt_questionAdjective = (TextView) findViewById(R.id.txt_questionAdjective);
        txt_questionAdjective.setText(questions.get(0));
        final int i = 0;
        grp_options = (RadioGroup) findViewById(R.id.grp_options);
        // Options are being prompted near the radioButtons.
        ((RadioButton) grp_options.getChildAt(0)).setText(optionAs.get(0));
        ((RadioButton) grp_options.getChildAt(1)).setText(optionBs.get(0));
        ((RadioButton) grp_options.getChildAt(2)).setText(optionCs.get(0));
        ((RadioButton) grp_options.getChildAt(3)).setText(optionDs.get(0));

        btn_nextQuestion = (Button) findViewById(R.id.btn_nextQuestion);

        btn_nextQuestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                promptTexts(optionAs, optionBs, optionCs, optionDs, questions,i);

            }

        });

    }

The data are being parsed from JSON data, no problem about parsing but the code about nextQuestion button is confusing. If you know a short way to do it or if you see what I don't see, I would appreciate your help.

Answer 1


Maybe I misunderstood your question, but couple of notes from me:

  1. Move all ArrayLists out of onCreate() to class variables.
  2. Don't pass all lists to the method promptTexts().
  3. Break your onCreate() code to some functions - at least one for reading, one for parsing (filling-in the arrays), etc.
  4. I hope you use TextView txt_optionA (and optionB, C, D) somewhere not in the pasted code. If you're not using them (which I suspect) - remove them.
  5. Don't lookup the RadioButtons every time - instead create some class variables with them (like RadioButton rbOptionA etc). The findViewById() lookup is generally expensive, so if you'll be using the view multiple times - just stick it into a variable.

Those won't have any performance impact (except super-minor from #2 and #5), but will make your code much cleaner and more readable.

EDIT: Extract the counter i as a member variable as well and don't pass it as part of the promptTexts() method. This way your next button will actually work :)