如何实时更新从api获取的值?

时间:2017-09-03 11:02:05

标签: android api android-asynctask radio-button

我正在制作一个测验应用程序,其中我正在显示问题,它是单选按钮中的四个选项,我想通过点击按钮将其更新为下一个ID问题逐个显示问题... ..我该怎么办?以及如何检查我想要在下一个活动中显示的分数? 应用程序在按钮列表器上崩溃

提前致谢!

QuizActivity:

public class Quiz extends AppCompatActivity {

    RadioGroup radioGroup;
    RadioButton optionOne,optionTwo,optionThree,optionFour;
    private TextView questionName;
    String question_name;
    String option1,option2,option3,option4;
    Button next_question;
    int first_question_index=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        questionName=(TextView)findViewById(R.id.question_name);
        radioGroup =(RadioGroup)findViewById(R.id.radioGroup);
        optionOne=(RadioButton)findViewById(R.id.answerOne);
        optionTwo=(RadioButton)findViewById(R.id.answerOne);
        optionThree=(RadioButton)findViewById(R.id.answerOne);
        optionFour=(RadioButton)findViewById(R.id.answerOne);
        next_question=(Button)findViewById(R.id.button_next_question);

        FetchLists fetchLists =new FetchLists();
        fetchLists.execute(10,0);

        next_question.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                first_question_index++;

            }
        }); }

    public class FetchLists extends AsyncTask<Integer, Void, String> {

        @Override
        protected String doInBackground(Integer... params) {


            String urlString = "http://aptronnoida.com/Aditya_July4/Demo/JAVA_FETCH.php";


            try {
                URL url = new URL(urlString);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                InputStream stream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
                String line = reader.readLine();
                String response = "";
                while (line != null) {
                    response += line;
                    line = reader.readLine();
                }

                JSONObject object = new JSONObject(response);
                JSONArray jsonArray = object.getJSONArray("data");

                    JSONObject list = (JSONObject) jsonArray.get(first_question_index);
                    question_name= list.getString("Question");
                    option1=list.getString("A1");
                    option2=list.getString("A2");
                    option3=list.getString("A3");
                    option4=list.getString("A4");

            } catch (Exception e) {
                e.printStackTrace();
            }
            return "quiz";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            questionName.setText(question_name);
            ((RadioButton) radioGroup.getChildAt(0)).setText(option1);
            ((RadioButton) radioGroup.getChildAt(1)).setText(option2);
            ((RadioButton) radioGroup.getChildAt(2)).setText(option3);
            ((RadioButton) radioGroup.getChildAt(3)).setText(option4);




        }
    }

}

1 个答案:

答案 0 :(得分:0)

public class Quiz extends AppCompatActivity {

    RadioGroup radioGroup;
    RadioButton optionOne,optionTwo,optionThree,optionFour;
    private TextView questionName;
    String question_name;
    String option1,option2,option3,option4;
    Button next_question;
    int first_question_index=0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        questionName=(TextView)findViewById(R.id.question_name);
        radioGroup =(RadioGroup)findViewById(R.id.radioGroup);
        optionOne=(RadioButton)findViewById(R.id.answerOne);
        optionTwo=(RadioButton)findViewById(R.id.answerOne);
        optionThree=(RadioButton)findViewById(R.id.answerOne);
        optionFour=(RadioButton)findViewById(R.id.answerOne);
        next_question=(Button)findViewById(R.id.button_next_question);


        next_question.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                first_question_index++;
                FetchLists fetchLists = new FetchLists();
                fetchLists.execute(10, 0);

            }
        });


        FetchLists fetchLists = new FetchLists();
        fetchLists.execute(10, 0);
    }

    public class FetchLists extends AsyncTask<Integer, Void, String> {

        @Override
        protected String doInBackground(Integer... params) {


            String urlString = "http://aptronnoida.com/Aditya_July4/Demo/JAVA_FETCH.php";


            try {
                URL url = new URL(urlString);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                InputStream stream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
                String line = reader.readLine();
                String response = "";
                while (line != null) {
                    response += line;
                    line = reader.readLine();
                }

                JSONObject object = new JSONObject(response);
                JSONArray jsonArray = object.getJSONArray("data");

                    JSONObject list = (JSONObject) jsonArray.get(first_question_index);

                    Log.d("ashu","JsonObject   "+list);


                    question_name= list.getString("Question");
                    option1=list.getString("A1");
                    option2=list.getString("A2");
                    option3=list.getString("A3");
                    option4=list.getString("A4");

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

            return "quiz";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);


            questionName.setText(question_name);

            ((RadioButton) radioGroup.getChildAt(0)).setText(option1);
            ((RadioButton) radioGroup.getChildAt(1)).setText(option2);
            ((RadioButton) radioGroup.getChildAt(2)).setText(option3);
            ((RadioButton) radioGroup.getChildAt(3)).setText(option4);





        }
    }

}