如何将数据设置为可扩展列表视图? [英] How to set data to Expandable List View?
问题描述
我尝试为我的应用程序添加可扩展列表视图.该列表包括标题和子列表.
I tried to add Expandable List View for my application. That list include header and sub list.
对于列表,使用网络服务加载的数据.
for the list, data loaded by using web service.
我使用了 Android 可扩展列表视图教程开发这个链接.
i used Android Expandable List View Tutorial link for develop this.
我成功加载了标题列表.但情况是,子列表包括数组列表中的所有数据.我只想加载与标题相关的内容.
i loaded header list successfully. but the case is, the sublist include all data in array list. i want to load only the things which related with the header.
例如:它显示酒店名称和所有酒店详细信息,再次显示酒店名称和所有酒店详细信息.
for an example: it display hotel name and all hotels details, again hotel name and all hotels details.
它通过网络服务成功加载了数据.我只想处理 arrayList.
It successfully load the data through the web service. i just want to handle the arrayList.
这是我使用的代码.
public void ListDrwaer() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
List<String> restData = new ArrayList<String>();
try {
JSONObject jsonResponse = new JSONObject(strJson1);
JSONArray jsonMainNode = jsonResponse.optJSONArray("restaurants");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String restName = jsonChildNode.optString("name");
String address = jsonChildNode.optString("address");
String mobile = jsonChildNode.optString("mobile");
String direction = jsonChildNode.optString("direction");
String bestTime = jsonChildNode.optString("bestTime");
String food = jsonChildNode.optString("food");
String dress = jsonChildNode.optString("dress");
String priceRange = jsonChildNode.optString("priceRange");
listDataHeader.add(restName);
restData.add(address);
restData.add(mobile);
restData.add(direction);
restData.add(bestTime);
restData.add(food);
restData.add(dress);
restData.add(priceRange);
listDataChild.put(listDataHeader.get(i), restData);
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error..." + e.toString(),
Toast.LENGTH_LONG).show();
}
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
}
有人可以帮我吗?
推荐答案
你需要为你的 for 循环中的每一项创建一个新的 restData
否则它会保留对同一个 for 的引用每个项目:
You need to create a new restData
for each item in your for loop otherwise it'll keep the reference to the same one for each item:
public void ListDrwaer() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
try {
JSONObject jsonResponse = new JSONObject(strJson1);
JSONArray jsonMainNode = jsonResponse.optJSONArray("restaurants");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String restName = jsonChildNode.optString("name");
String address = jsonChildNode.optString("address");
String mobile = jsonChildNode.optString("mobile");
String direction = jsonChildNode.optString("direction");
String bestTime = jsonChildNode.optString("bestTime");
String food = jsonChildNode.optString("food");
String dress = jsonChildNode.optString("dress");
String priceRange = jsonChildNode.optString("priceRange");
List<String> restData = new ArrayList<String>();
restData.add(address);
restData.add(mobile);
restData.add(direction);
restData.add(bestTime);
restData.add(food);
restData.add(dress);
restData.add(priceRange);
listDataHeader.add(restName);
listDataChild.put(restName, restData);
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error..." + e.toString(),
Toast.LENGTH_LONG).show();
}
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
}
这篇关于如何将数据设置为可扩展列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!