我正在开发 Android 中的客户端-服务器项目。我需要从给定的 URL 解析 JSON 并将其放在ExpandableListView
. 在 JSON 响应中,我正在获取包含数组的节点的数据。所以,我必须将 main 放在JsonArray
Groups 中ExpandableListView
。它工作正常。
我可以找出有多少组来自 JSON,但是当我尝试根据来自服务器的数据将孩子放入每个组中时,就会出现问题。我的代码只将第一个孩子放在第一组中,但是当我点击第二个孩子时,它显示错误。
我需要在各自的组内放置“ID”和“用户名”(例如:电子邮件、传真、上传)。我的代码可以将一个子信息放在一组中,但是当我单击第二组时,它会崩溃。任何形式的帮助将不胜感激。
所以,我的查询是,如何根据服务器数据动态创建组并在每个子项中放置子数据?
这是日志中的错误:
04-07 17:49:02.194: E/AndroidRuntime(13285): FATAL EXCEPTION: main
04-07 17:49:02.194: E/AndroidRuntime(13285): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
04-07 17:49:02.194: E/AndroidRuntime(13285): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
04-07 17:49:02.194: E/AndroidRuntime(13285): at java.util.ArrayList.get(ArrayList.java:304)
04-07 17:49:02.194: E/AndroidRuntime(13285): at android.widget.SimpleExpandableListAdapter.getChildrenCount(SimpleExpandableListAdapter.java:255)
04-07 17:49:02.194: E/AndroidRuntime(13285): at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:561)
04-07 17:49:02.194: E/AndroidRuntime(13285): at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:682)
04-07 17:49:02.194: E/AndroidRuntime(13285): at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:561)
04-07 17:49:02.194: E/AndroidRuntime(13285): at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:521)
04-07 17:49:02.194: E/AndroidRuntime(13285): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
04-07 17:49:02.194: E/AndroidRuntime(13285): at android.widget.AbsListView$1.run(AbsListView.java:3168)
04-07 17:49:02.194: E/AndroidRuntime(13285): at android.os.Handler.handleCallback(Handler.java:605)
04-07 17:49:02.194: E/AndroidRuntime(13285): at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 17:49:02.194: E/AndroidRuntime(13285): at android.os.Looper.loop(Looper.java:137)
04-07 17:49:02.194: E/AndroidRuntime(13285): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-07 17:49:02.194: E/AndroidRuntime(13285): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 17:49:02.194: E/AndroidRuntime(13285): at java.lang.reflect.Method.invoke(Method.java:511)
JSON response
{ "email":[
{"ID":"123","username":"dipakadmin"},
{"ID":"3233","username":"raju"},
{"ID":"5445","username":"hussain"}
]
,
"fax":[
{"ID":"6665","username":"mohammad"},
{"ID":"9877","username":"raj"},
{"ID":"87655","username":"aryan"}
]
,
"upload":
[
{"ID":"132322","username":"raja"},
{"ID":"544333","username":"bala"},
]
}
这只是 JSON 响应的一个示例。真实的响应有很多标签。
ExpandableListActivity 的代码示例如下:
package com.ExpandableListActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class DocumentCenter extends ExpandableListActivity {
public static JSONObject jsonDocu=null;
JSONArray jArray = null;
JSONObject json_data = null;
JSONObject root = null;
List<List<Map<String, String>>> childs;
List<Map<String, String>> child1;
List<Map<String, String>> child2;
List<Map<String, String>> child3;
SimpleExpandableListAdapter adapter;
Map<String, String> childdata1;
public static String url3=null;
String strEmailNull=null;
String strFaxNull=null;
String strUploadNull=null;
public String user=null;
TextView txtMLSID;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
user= TabBarExample.getJsonUser;
Log.e("log_tag","USERNAME On Document : "+user);
List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
child1 = new ArrayList<Map<String, String>>();
child2 = new ArrayList<Map<String, String>>();
child3 = new ArrayList<Map<String, String>>();
url3 ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
Log.e("log_tag","URL on DOCUMENT : "+url3);
try {
jsonDocu = JSONfunctions1.getJSONfromURL(url3);
JSONArray ary = jsonDocu.names();
Log.e("log_tag","ary : "+ary .toString());
for (int i1 = 0; i1 < ary.length(); i1++) {
String value = ary.get(i1).toString();
Log.e("log_tag","value : "+value);
strEmailNull= jsonDocu.getString(value);
if (!strEmailNull.equals("null")){
try{
Map<String, String> group1 = new HashMap<String, String>();
group1.put("group", value);
//group1.put("group", value+(i1+1));
groups.add(group1);
JSONArray email = jsonDocu.getJSONArray(value);
for(int i=0;i<email.length() ;i++){
childdata1 = new HashMap<String, String>();
JSONObject e = email.getJSONObject(i+1);
childdata1.put("child", e.getString("ID"));
childdata1.put("child1", "Name: "+e.getString("name"));
childdata1.put("child2", "Size: "+e.getString("size"));
childdata1.put("child3", "Update: "+e.getString("lastUpdate"));
child1.add(childdata1);
childs = new ArrayList<List<Map<String, String>>>();
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
childs.add(child1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
adapter = new SimpleExpandableListAdapter(
this, groups, R.layout.groups2, new String[] { "group" },
new int[] { R.id.group }, childs, R.layout.childs2,
new String[] { "child1","child2" ,"child3"}, new int[] { R.id.child1, R.id.child2, R.id.child3});
setListAdapter(adapter);
}
@Override
public boolean setSelectedChild(int groupPosition, int childPosition,
boolean shouldExpandGroup) {
//do something
Log.e("log_tag","setSelectedChild: ");
return super.setSelectedChild(groupPosition, childPosition,
shouldExpandGroup);
}
@Override
public void setSelectedGroup(int groupPosition) {
//do something
Log.e("log_tag","setSelectedGroup: ");
super.setSelectedGroup(groupPosition);
}
}