私は JSON 文字列を持っています。解析しようとしていて、ListView に入れようとしています。コードはエラーをスローしませんが、どちらも機能していません。どこが間違っているのかを判断できません。以下は私のコードです:
protected void onPostExecute(String result) {
super.onPostExecute(result);
str = result;
Toast.makeText(getApplicationContext(),""+str, Toast.LENGTH_LONG).show(); // Able to toast the JSON string (str) here.
mylist = new ArrayList<HashMap<String, String>>();
map = new HashMap<String, String>();
try {
JSONArray jArray = new JSONArray(str);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
j_id = jObject.getString(ID);
j_make = jObject.getString(MAKE);
j_model = jObject.getString(MODEL);
j_version = jObject.getString(VERSION);
j_price = jObject.getString(PRICE);
j_reg_plc = jObject.getString(PLACE_REG);
Toast.makeText(getApplicationContext(),"After assign", Toast.LENGTH_LONG).show();
data = "Make: "+ j_make + "\nModel: " + j_model + "\nVersion: " + j_version + "\nPrice: " + j_price
+ "\nCity: " + j_reg_plc;
map.put("car", data);
mylist.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
// list = (ListView) findViewById(R.id.listView1);
Toast.makeText(getApplicationContext(),""+mylist, Toast.LENGTH_LONG).show();
String[] from = new String[] { "car"};
int[] to = new int[] { R.id.text1 };
ListAdapter adapt = new SimpleAdapter(view_cars.this, mylist,R.layout.text_adaptr, from, to);
list.setAdapter(adapt);
}
for ループが突然終了しているようです。ネットワークから受信している配列の長さが 2 であるため、2 回実行されていません。取得しているJSON配列のスクリーンショットを添付しました
詳細については、コードのコメントを確認してください。