所以我正在为android做一个测验,我有一个问题。我在 JSONArrays 中解析来自服务器的问题和答案并得到我想要的。因此,当我开始活动时,将显示第一个问题及其相关答案,TextView 中的问题和 ListView 中的答案。但问题来了,我想在选择答案时看到下一个问题,所以我想循环遍历 JSONObject 并在前一个已回答时显示一个新问题。有人知道我该怎么做吗?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
listView = (ListView)findViewById(R.id.list_answers);
gametv = (TextView)findViewById(R.id.text_question);
Intent intent = getIntent();
final String gameID = intent.getStringExtra("gameID");
final int questionAmount = intent.getIntExtra("amount", 0);
final Set<String> set = new HashSet<String>();
JSONArray quiz = null;
try {
JSONObject json = new JSONObject();
json.put("gameID", gameID);
quiz = SendHttp.parseHttp(qURL, "result", json);
displayQuiz(quiz, questionAmount);
} catch (JSONException e) {
e.printStackTrace();
}
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view.findViewById(R.id.tv_1);
String answerString = tv.getText().toString();
set.add(answerString);
Toast.makeText(Game.this, answerString, Toast.LENGTH_SHORT).show();
Log.w("svarat", set.toString());
}
});
}
private void displayQuiz(JSONArray jsona, int amount) throws JSONException {
gametv.setText(jsona.getJSONObject(0).getString("question"));
int numberOfAnswers = Integer.parseInt(jsona.getJSONObject(0).getString("n"));
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < numberOfAnswers; i++) {
String qans = jsona.getJSONObject(0).getString(String.valueOf(i+1));
JSONObject answer = new JSONObject(qans);
map.put("answerID", answer.getString("answerID"));
map.put("answer", answer.getString("answer"));
mylist.add(map);
map = new HashMap<String, String>();
}
SimpleAdapter sadapter = new SimpleAdapter(this, mylist, R.layout.quizlist, new String[] {"answerID","answer"},
new int[] {R.id.tv_1, R.id.tv_2});
listView.setAdapter(sadapter);
}