Android 如何将内部JSONArray设置为ListView?
我正在使用这样的JSON,我必须在列表视图中设置每本书的名称、书籍和价格。我在列表中只设置了名称和书籍,但我不知道如何在ListView的每个项目中设置内部JSONArray价格,与每个名称对应的价格有意义吗 那么,我该怎么做呢?有什么想法吗?。感谢您的时间和回答:Android 如何将内部JSONArray设置为ListView?,android,json,Android,Json,我正在使用这样的JSON,我必须在列表视图中设置每本书的名称、书籍和价格。我在列表中只设置了名称和书籍,但我不知道如何在ListView的每个项目中设置内部JSONArray价格,与每个名称对应的价格有意义吗 那么,我该怎么做呢?有什么想法吗?。感谢您的时间和回答: { "users”: [ { “name” : “Jaime”, “books” : "3”, “price” : [
{ "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>
实现这一点的简单方法是Gson-parsetry使用getter/setter方法创建两个单独的Bean/Model类,其中一个类包含带有ArrayList的图书详细信息,另一个类包含价格详细信息