第一个问题和答案在我设置文本视图时是否重复?



我的mysql数据库中有很多问题和答案,我在设置它们时正在创建测验应用程序,我使用json通过mysql数据库获取所有问题和答案,然后我得到的第一个问题和答案。如何设置所有问题和答案,如何在代码中使用循环,请帮忙?

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() + "nn");
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() + "nn");
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);
}
}

首先学习如何使用RecyclerView这里是链接 回收器视图教程 并学习如何解析 json json 解析教程 学习这 2 个教程肯定会帮助您解决这个问题以及将来。

最新更新