gpt4 book ai didi

android - 如何在 android 中从 json 动态创建可扩展 ListView 中的组和子项?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:19 25 4
gpt4 key购买 nike

我正在 Android 中开发客户端-服务器项目。我需要从给定的 URL 解析 JSON 并将其放在 ExpandableListView 中。在 JSON 响应中,我正在获取包含数组的节点的数据。因此,我必须将主要的 JsonArray 放置到 ExpandableListView 中的组中。它工作正常。

我可以从 JSON 中找出有多少组,但是当我试图根据来自服务器的数据将 child 放入每个组时,问题就出现了。我的代码只将第一个 child 放在第一组中,但是当我点击第二个 child 时,它显示错误。

我需要将“ID”和“用户名”放在相应的组中(例如:电子邮件、传真、上传)。我的代码可以将一个 child 的信息放在一组中,但是当我单击第二组时,它崩溃了。我们将不胜感激任何形式的帮助。

那么,我的问题是,如何根据服务器数据动态创建组并将子数据放入每个子项中?

这是日志中的错误:

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 响应

{   "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);
}
}

最佳答案

下面是我的做法。当然,您可以像解析一样使用解析。但是,我会使用 jackson 库并根据响应创建一个对象类,并使用 ObjectMapper,它看起来更优雅并且可以节省您的解析时间。您可以根据需要在此处添加更多字段,例如内部传真和电子邮件类和列表:

public class ResponseObject {

private List<Email> email;

/**
* @return the email
*/
public List<Email> getEmail() {
return email;
}

/**
* @param email
* the email to set
*/
public void setEmail(List<Email> email) {
this.email = email;
}

public static class Email {
private Integer ID;
private String username;

/**
* @return the ID
*/
public Integer getID() {
return ID;
}

/**
* @param ID
* the ID to set
*/
public void setID(Integer ID) {
ID = ID;
}

/**
* @return the username
*/
public String getUsername() {
return username;
}

/**
* @param username
* the username to set
*/
public void setUsername(String username) {
this.username = username;
}
}
}

然后像这样使用 BaseExpandableListAdapter,根据需要进行更改和添加 View :

import java.util.ArrayList;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

public class ResponseAdapter extends BaseExpandableListAdapter {
private ArrayList<ResponseObject> responseList = new ArrayList<ResponseObject>();

public ResponseAdapter(ArrayList<ResponseObject> responseObject) {
this.responseList = responseObject;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
int ID = responseList.get(groupPosition).getEmail().get(childPosition)
.getID();
String username = responseList.get(groupPosition).getEmail()
.get(childPosition).getUsername();
return null;
}

@Override
public int getChildrenCount(int groupPosition) {
return responseList.get(groupPosition).getEmail().size();
}

@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}

@Override
public int getGroupCount() {
return responseList.size();
}

@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View parent, ViewGroup viewGroup) {
// Email group goes here
return null;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}

}

关于android - 如何在 android 中从 json 动态创建可扩展 ListView 中的组和子项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10054711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com