-1
HashMap<String, String> map = new HashMap<>();
JSONObject jsonResponse = new JSONObject(response);
HashMap<String, String> question1 = readJsonObject(jsonResponse.getJSONObject("1"), map);
HashMap<String, String> question2 = readJsonObject(jsonResponse.getJSONObject("2"), map);
HashMap<String, String> question3 = readJsonObject(jsonResponse.getJSONObject("3"), map);
q1.setText(question1.get("question")); //q1,q2,q3 are textviews
q2.setText(question2.get("question"));
q3.setText(question3.get("question"));
這是readJsonObject方法/函數:
private HashMap<String,String> readJsonObject(JSONObject jsonObject,HashMap<String, String> map) throws JSONException {
map.put("question", jsonObject.getString("question"));
map.put("optiona", jsonObject.getString("optiona"));
map.put("optionb", jsonObject.getString("optionb"));
map.put("optionc", jsonObject.getString("optionc"));
map.put("optiond", jsonObject.getString("optiond"));
map.put("correct", jsonObject.getString("correct"));
return map;
}
但我得到在Q1 Q2 Q4 TextViews相同的字符串值。請解決這個問題。
響應從所述服務器獲取:
{
"0":[],
"success":true,
"1":{ "question":"question3",
"optiona":"optiona",
"optionb":"optionb",
"optionc":"optionc",
"optiond":"optiond",
"correct":"optiona"},
"2":{ "question":"question1",
"optiona":"optiona",
"optionb":"optionb",
"optionc":"optionc",
"optiond":"optiond",
"correct":"optiond"},
"3":{ "question":"question4",
"optiona":"optiona",
"optionb":"optionb",
"optionc":"optionc",
"optiond":"optiond",
"correct":"optionc"}
}
獲取從服務器不同的值響應但Q1,Q2,Q3表示相同的值。爲什麼?請解決此問題
如果我在每個readJsonObject()textview變爲空白後添加map.clear(),並且如果我在readJsonObject()中添加map.clear,則q1,q2中的變化仍然不變, q3
如果我在每次調用readJsonObject獲取正確答案時使用不同的hashmap,但我認爲效率不高。對?
清除你進入readJsonObject –
如果我每天readJsonObject後加入map.clear()()的TextView變成空白,,,,,如果我添加地圖HashMap中每次.clear in readJsonObject()在q1,q2,q3中沒有改變仍然是相同的值 –
如果我在每次調用readJsonObject時都使用不同的hashmap得到正確的答案但我認爲效率不高。對? –