我正在使用这样的JSON,我必须将每本书的"name", "books"
和"price"
设置为ListView。我已在我的列表中设置了"name"
和"books"
但我不知道如何在每个项目中设置内部 JSONArray "price"
我的ListView对应于每个名称(这有意义吗?)。
那么,我该怎么做?有任何想法吗?。感谢您的时间和答案:)
{ "users”:
[ { “name” : “Jaime”,
“books” : "3”,
“price” :
[
{"1”: “90.00" },
{"2”: ”90.00" },
{"3”: "95.00”}
],
},
{ “name” : “Carl”,
“books":"5”,
“price”:
[
{"1”: "88.00”},
{"2”: "100.00”},
{"3”: "100.00”},
{"4”: "100.00”},
{"5”: "100.00”}
]
}
}
我想做的一个小例子是:
----------------------------------------------
----------------------------------------------
| Jaime books: 3 |
| 90 - 90 - 95 |
| Carls books: 5 |
| 88 - 100 - 100 - 100 - 100 |
-------------------------------------------
我的代码是:
try {
JSONArray users = jsonObject.getJSONArray("users");
ArrayList<HashMap<String,String>> names = new ArrayList<>();
for(int i = 0; i < users.length(); i++){
HashMap<String, String> info = new HashMap<>();
JSONObject infoUsers= users.getJSONObject(i);
JSONArray price = infoUsers.optJSONArray("price");
String name = infoUsers.getString("name");
String books = infoUsers.getString("books");
info.put("name", name);
info.put("books", books);
for(int g = 0; g < price.length(); g++){
Log.d("Price ", g + " " + price.optString(g));
}
names.add(info);
}
String[] keys = {KEY_NAME,KEY_MATERIA};
int[] ids = {R.id.name, R.id.books};
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getApplicationContext(),
names, R.layout.name_and_books, keys, ids);
myList.setAdapter(simpleAdapter);
} catch (JSONException e) {
Log.e("Application ", "Error parsing data " + e.toString());
}
和我的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="25dp"
android:text="Name"/>
<TextView
android:id="@+id/books"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="10dp"
android:text="Books" />
</LinearLayout>
</LinearLayout>