我在让我的一个 JSONObject 显示在 ListView 中时遇到了一些问题。我正在使用标准的 JSON 解析器、异步任务等,除了一个特定的字符串(即总数)之外,所有内容都可以正常工作。这是我的代码片段:
杰森:
{
"item": "Item name",
"quantity": "67",
"unit": "m",
"total": "603.00"
},
人造人:
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
items = jsonObj.getJSONArray(TAG_ITEMS);
for (int a = 0; a < items.length(); a++) {
JSONObject l = items.getJSONObject(a);
String item = l.getString(TAG_ITEM_NAME);
String unit = l.getString(TAG_UNIT);
String total = l.getString(TAG_TOTAL);
String quantity = l.getString(TAG_QUANTITY);
Log.d("item got: ", "> " + item);
Log.d("unit got: ", "> " + unit);
Log.d("total got: ", "> " + total);
Log.d("qty got: ", "> " + quantity);
HashMap<String, String> myItems = new HashMap<String, String>();
myItems.put(TAG_ITEM_NAME, item);
myItems.put(TAG_UNIT, unit);
myItems.put(TAG_TOTAL, total);
myItems.put(TAG_QUANTITY, quantity);
itemList.add(myItems);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(ItemsActivity.this,
itemList, R.layout.item_list_item,
new String[] { TAG_ITEM_NAME, TAG_UNIT, TAG_TOTAL, TAG_QUANTITY },
new int[] { R.id.itemName, R.id.unit, R.id.total, R.id.quantity});
setListAdapter(adapter);
}
.XML:
<TextView
android:id="@+id/itemName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/total"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/unit"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/quantity"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
现在,根据日志,所有四个都"得到"了,但是当我尝试将它们放入我的 ListView 时,总数(十进制数字)将不会显示。单个数字和其他非十进制数字完美显示,并且所有内容都是 JSON 中的字符串,那么为什么我不能显示我的总数呢?
困惑。。。
使用这个,
findViewById(R.id.itemName).setText(myItems.get(TAG_ITEM_NAME));
但在此之前,我的物品可以访问。