2017-09-03 64 views
0

我做了一個測驗應用程序,其中我顯示的問題,它是單選按鈕中的四個選項,我想通過將它更新到下一個id問題點擊按鈕來逐個顯示問題.....我應該如何去做?以及我應該如何檢查分數,以便我將在下一個活動中顯示? 該應用程序崩潰的按鈕排列器如何更新從API實時獲取值?

在此先感謝!

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




     } 
    } 

} 

回答

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





     } 
    } 

}