我创建了可扩展列表视图。它包含父项和子项。从Json检索父值和子值。子项正在加载正常,但只有Json中的最后一个值显示在父项中。如何在父母中显示所有项目?
class AsyncT1 extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://abservetechdemo.com/projects/android/food_app/public/mobile/restaurant/resdetails");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("res_id","1"));
// nameValuePairs.add(new BasicNameValuePair("status","veg"));
nameValuePairs.add(new BasicNameValuePair("main_cat",childname));
// nameValuePairs.add(new BasicNameValuePair("group_id", group_id));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());
/* execute */
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
InputStream inputStream = response.getEntity().getContent();
InputStreamToStringExample str = new InputStreamToStringExample();
String responseServer = str.getStringFromInputStream(inputStream);
Log.d("response", "response -----" + responseServer);
JSONObject jsonRootObject = new JSONObject(responseServer);
JSONObject jsonobject = jsonRootObject.getJSONObject("restaurants");
JSONObject questionMark = jsonRootObject.getJSONObject("restaurants");
Iterator keys = questionMark.keys();
Log.d("key--", String.valueOf(keys));
while(keys.hasNext()) {
// loop to get the dynamic key
String k = (String)keys.next();
Group gru = new Group();
gru.setName(k);
// do something here with the value...
final ArrayList<Group> list = new ArrayList<Group>();
ArrayList<Child> ch_list;
ch_list = new ArrayList<Child>();
JSONArray ja = questionMark.getJSONArray(k);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
Child ch = new Child();
ch.setName(jo.getString("item_name"));
ch_list.add(ch);
} // for loop end
gru.setItems(ch_list);
list.add(gru);
getActivity(). runOnUiThread(new Runnable() {
@Override
public void run() {
adapter = new ExpandListAdapter(
getActivity(), list);
ExpandList.setAdapter(adapter);
}});
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
}
}
答案 0 :(得分:2)
这样做:
class AsyncT1 extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
//YOUR CODE
try
{
// YOUR CODE
Log.d("key--", String.valueOf(keys));
// better to initialize main list out side any loop so it will
//not get initialized with every iteration of loop..!!
final ArrayList<Group> list = new ArrayList<Group>();
while (keys.hasNext()) {
// loop to get the dynamic key
String k = (String) keys.next();
Group gru = new Group();
gru.setName(k);
// do something here with the value...
ArrayList<Child> ch_list;
ch_list = new ArrayList<Child>();
JSONArray ja = questionMark.getJSONArray(k);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
Child ch = new Child();
ch.setName(jo.getString("item_name"));
ch_list.add(ch);
} // for loop end
gru.setItems(ch_list);
list.add(gru);
}
// set the adapter after the loop has finished the execution
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
adapter = new ExpandListAdapter(
getActivity(), list);
ExpandList.setAdapter(adapter);
}
});
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
}
}
此处// YOUR CODE
表示您无需更改代码中已存在的代码。
我所做的更改主要包括:您不需要在内部设置适配器 while循环让列表将所有数据初始化,然后我们将 将适配器设置为外部
while loop