2017-08-18 1 views
0

私はmysqlデータベースで多くの質問と回答を持っています。私はそれらを設定するときにクイズアプリを作成しています。私はすべての質問を受け取り、mysqlデータベースを使ってjsonを使って答えます。すべての質問と回答を設定するにはどうすればよいですか?私のコードでループを使用してどうすれば助けてくれますか?テキストビューに設定すると、最初の質問と回答が繰り返されますか?

json_string=getIntent().getExtras().getString("j"); 
    try { 
     jsonObject=new JSONObject(json_string); 
     jsonArray=jsonObject.getJSONArray("server_response"); 
     if(jsonArray.length()!=0){ 
      JSONObject JO=jsonArray.getJSONObject(count); 
      Ques = JO.getString("QuestionText"); 
      Options=JO.getString("Options").split("##"); 
      ans = Integer.parseInt(JO.getString("CorrectAnswer")); 

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

    if (savedInstanceState == null) { 
     Bundle extras = getIntent().getExtras(); 
     if (extras == null) { 

     } else { 

      Answers=extras.getString("urAnswers"); 
     } 
    } 
    l=(LinearLayout)findViewById(R.id.Review); 
    ScrollView scrollView= new ScrollView(this); 
    LinearLayout Lin =new LinearLayout(this); 
    int i; 
    for (i=0;i<jsonArray.length();i++){ 
     TextView Qus = new TextView(this); 
     Qus.setTextSize(20); 
     Qus.setTextColor(Color.WHITE); 
     Qus.setPadding(0, 20, 0, 0); 
     Qus.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     TextView OptA = new TextView(this); 
     TextView OptB = new TextView(this); 
     TextView OptC = new TextView(this); 
     TextView OptD = new TextView(this); 
     TextView urans = new TextView(this); 
     TextView correctans = new TextView(this); 
     Qus.setText (" Question. No.(" + (QuestionNo + 1) + ").\n " + Ques); 
     OptA.setText("(a). " + Options[0].toString() + "\n"); 
     OptB.setText("(b). " + Options[1].toString() + "\n"); 
     OptC.setText("(c). " + Options[2].toString() + "\n"); 
     OptD.setText("(d). " + Options[3].toString() + "\n\n"); 

     String a = ""; 
     int color = 0; 
     String Answers1 = Answers.substring(i, i + 1); 
     String correct = String.valueOf(ans); 
     if (Answers1.equals("5")) { 
      a = "UnAnswered"; 
      color = Color.YELLOW; 
     } 
     if (Answers1.equals("1")) { 
      a = "A"; 
      if (correct.equals("1")) 
       color = Color.GREEN; 
      else 
       color = Color.RED; 
     } 
     if (Answers1.equals("2")) { 
      a = "B"; 
      if (correct.equals("2")) 
       color = Color.GREEN; 
      else 
       color = Color.RED; 
     } 
     if (Answers1.equals("3")) { 
      a = "C"; 
      if (correct.equals("3")) 
       color = Color.GREEN; 
      else 
       color = Color.RED; 
     } 
     if (Answers1.equals("4")) { 
      a = "D"; 
      if (correct.equals("4")) 
       color = Color.GREEN; 
      else 
       color = Color.RED; 
     } else { 
     } 

     String b = ""; 


     if (correct.equals("1")) { 
      b = "A"; 
     } 
     if (correct.equals("2")) { 
      b = "B"; 
     } 
     if (correct.equals("3")) { 
      b = "C"; 
     } 
     if (correct.equals("4")) { 
      b = "D"; 
     } else { 

     } 

     urans.setText("Your Answer :" + a.toString() + "\n"); 
     urans.setTextSize(18); 
     urans.setTextColor(color); 
     correctans.setText("Correct Answer :" + b.toString() + "\n\n"); 
     correctans.setTextSize(18); 
     correctans.setTextColor(Color.GREEN); 
     Lin.addView(Qus); 
     Lin.addView(OptA); 
     Lin.addView(OptB); 
     Lin.addView(OptC); 
     Lin.addView(OptD); 
     Lin.addView(urans); 
     Lin.addView(correctans); 
     OptA.setTextSize(18); 
     //OptA.setPadding(0,30,0,0); 
     OptA.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams 
       .WRAP_CONTENT)); 
     OptA.setTextColor(Color.WHITE); 
     OptB.setTextSize(18); 
     OptB.setTextColor(Color.WHITE); 
     //OptA.setPadding(0,30,0,0); 
     OptB.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams 
       .WRAP_CONTENT)); 
     OptC.setTextSize(18); 
     OptA.setPadding(0, 30, 0, 0); 
     OptC.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams 
       .WRAP_CONTENT)); 
     OptC.setTextColor(Color.WHITE); 
     OptD.setTextSize(18); 
     //OptA.setPadding(0,30,0,0); 
     OptD.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams 
       .WRAP_CONTENT)); 
     OptD.setTextColor(Color.WHITE); 
    } 

    scrollView.addView(Lin); 
    scrollView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 
      LinearLayout.LayoutParams.FILL_PARENT)); 
    scrollView.setPadding(20,10,10,20); 
    Lin.setOrientation(LinearLayout.VERTICAL); 
    getWindow().setBackgroundDrawableResource(R.drawable.back3); 
    setContentView(scrollView); 
} 

} 
+1

実行時にscrollViewを使用してレイアウトを作成しています。私はあなたのjsonをシリアル化し、代わりにRecyclerViewを使う方が良いと思います。 –

+1

使い方ヘルプ –

+1

これを参照してください:: http://www.truiton.com/2015/02/android-recyclerview-tutorial/ –

答えて

0

ここRecyclerViewの使用方法を学ぶの最初にリンクRecyclerView tutorialであり、これらの2チュートリアルを学習JSON json parsing tutorialを解析する方法を学ぶには、確かにこの問題のためだけでなく、将来的にお手伝いをします。

関連する問題