将文本设置为从API

时间:2017-09-03 06:49:26

标签: php android android-asynctask radio-button

我正在制作一个测验应用程序,其中我正在显示问题及其选项,我想在从api检索文本后将文本设置为单选按钮和textiew,我不知道如何设置文本它警告我这样做,因为我已经在我的模型类中使用值布尔值,我声明了它。

Api:http://aptronnoida.com/Aditya_July4/Demo/JAVA_FETCH.php

QuizActivity:

    public class Quiz extends AppCompatActivity {

        public List<QuizModel> quizModels = new ArrayList<>();
        QuizModel quizModel;
        RadioGroup radioGroup;
        RadioButton optionOne,optionTwo,optionThree,optionFour;
        private TextView questionName;

        @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);
            FetchLists fetchLists =new FetchLists();
            fetchLists.execute(10,0);

        }

        public class FetchLists extends AsyncTask<Integer, Void, List<QuizModel>> {

            @Override
            protected List<QuizModel> doInBackground(Integer... params) {

                int count = params[0];
                int offset = params[1];

                String urlString = "http://aptronnoida.com/Aditya_July4/Demo/JAVA_FETCH.php";
                urlString = urlString + "&count=" + count + "&offset=" + offset;

                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 emailLists = object.getJSONArray("data");

                    for (int i = 0; i < emailLists.length(); i++) {
                        JSONObject list = (JSONObject) emailLists.get(i);

                         quizModel.model_question_name= list.getString("Question");
//how to set text for radiobuttons here
                        quizModel.model_answer_one = list.getString("A1");
                        quizModel.model_answer_two = list.getString("A2");
                        quizModel.model_answer_three = list.getString("A3");
                        quizModel.model_answer_four=list.getString("A4");

                        quizModels.add(quizModel);
                    }

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

            @Override
            protected void onPostExecute(List<QuizModel> result) {
                super.onPostExecute(result);

                Toast.makeText(Quiz.this,"OnPostMethod Called",Toast.LENGTH_SHORT).show();
                Log.d("ashu","OnPostMethod");

            }
        }

    }

QuizModel:

public class QuizModel {

    public String model_question_name;
    public boolean model_answer_one,model_answer_two,model_answer_three,model_answer_four;

    public QuizModel(String model_question_name, boolean model_answer_one, boolean model_answer_two, boolean model_answer_three, boolean model_answer_four) {
        this.model_question_name = model_question_name;
        this.model_answer_one = model_answer_one;
        this.model_answer_two = model_answer_two;
        this.model_answer_three = model_answer_three;
        this.model_answer_four = model_answer_four;
    }
}

0 个答案:

没有答案