android - 在单个子行中包含多个数据的可扩展 ListView

标签 android json expandablelistview expandablelistadapter

我想显示组和子数据,我指的是这个链接 http://www.journaldev.com/9942/android-expandablelistview-example-tutorial ,但是本教程在 subview 和父 View 中只有一个项目,我想在父 View 和 subview 中设置多个项目。谁能帮我做这个?

for group 1)
name, location, number
for child 1)
name and id

我的 json 数据是,

"stds": [
    {
      "stopNum": "0",
      "stopName": "one",
      "loc": "23.097278,72.446139"
    },
    {
      "stopNum": "1",
      "stopName": "theatre",
      "loc": "23.073028,72.5245",
      "students": [
        {
          "studentName": "Luke",
          "studentId": "58eb423bcf51a2bd759a740a"
        }
      ]
    },
    {
      "stopNum": "2",
      "stopName": "flyover",
      "loc": "23.07,72.521917",
      "students": [
        {
          "studentName": "Leia",
          "studentId": "58eb423ccf51a2bd759a740b"
        }
      ]
    },
    {
      "stopNum": "3",
      "stopName": "mall",
      "loc": "23.076222,72.50775",
      "students": [
        {
          "studentName": "Advik",
          "studentId": "58eb423ccf51a2bd759a740d"
        },
        {
          "studentName": "Bipasha",
          "studentId": "58eb423ccf51a2bd759a740e"
        }
      ]
    },
    {
      "stopNum": "4",
      "stopName": "alpha",
      "loc": "23.093639,72.495972",
      "students": [
        {
          "studentName": "Aarav",
          "studentId": "58eb423ccf51a2bd759a740c"
        }
      ]
    },
    {
      "stopNum": "5",
      "stopName": "reliance",
      "loc": "23.097278,72.446139",
      "students": []
    }
  ]

我是这样解析的,

String stopNum,stopName,stopLoc,studentName,studentId;
JSONArray routedataArray = generalFunc.getJsonArray(objects,"stds");

for (int i = 0; i < routedataArray.length(); i++) {
JSONObject jsonObject,studentobject;
JSONArray studentArray;
try {
jsonObject = routedataArray.getJSONObject(i);
stopNum=generalFunc.getStringFromJobject(jsonObject, "stopNum");
stopName=generalFunc.getStringFromJobject(jsonObject, "stopName");
stopLoc=generalFunc.getStringFromJobject(jsonObject, "loc");
studentArray=jsonObject.getJSONArray("students");

for(int j=0;j<studentArray.length();j++){
studentobject=studentArray.getJSONObject(j);
studentName=generalFunc.getStringFromJobject(studentobject, "studentName");
studentId=generalFunc.getStringFromJobject(studentobject, "studentId");
mapchild=new HashMap<>();
mapchild.put("studentName",studentName);
mapchild.put("studentId",studentId);
}

mapheader = new HashMap<>();
mapheader.put("stopNum",stopNum);
mapheader.put("stopName",stopName);
mapheader.put("loc",stopLoc);


} catch (JSONException e) {
e.printStackTrace();
}
}

最佳答案

这是一个工作示例,像这样创建模型类

public class routedata {
    private String stopNum,stopName, stopLoc;
    private ArrayList<studentData> ch_list;

    public String getStopNum() {
        return stopNum;
    }

    public String getStopName() {
        return stopName;
    }

    public String getStopLoc() {
        return stopLoc;
    }


    public ArrayList<studentData> getCh_list() {
        return ch_list;
    }

    public routedata(String stopNum, String stopName, String stopLoc, ArrayList<studentData> ch_list) {
        this.stopLoc=stopLoc;
        this.stopName=stopName;
        this.stopNum=stopNum;
        this.ch_list=ch_list;
    }

    public ArrayList<studentData> getItems() {
        return ch_list;
    }
}

studentData.java

public class studentData {
    String studentName,studentId;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public studentData(String studentName, String studentId) {
        this.studentId=studentId;
        this.studentName=studentName;
    }
}

像这样改变适配器..

public class RouteDataExpListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private ArrayList<routedata> list;


    public RouteDataExpListAdapter(Context context, ArrayList<routedata> list) {
        this.context=context;
        this.list=list;
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {

        ArrayList<studentData> chList = list.get(listPosition).getItems();
        return chList.get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getChildView(int listPosition, final int expandedListPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        studentData child = (studentData) getChild(listPosition, expandedListPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_list_item, null);
        }
        TextView std_details = (TextView) convertView
                .findViewById(R.id.expandedListItem);

        std_details.setText(child.getStudentId()+" "+child.getStudentName());


        return convertView;
    }

    @Override
    public int getChildrenCount(int listPosition) {

        ArrayList<studentData> chList = list.get(listPosition).getItems();
        return chList.size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return list.get(listPosition);
    }

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

    @Override
    public long getGroupId(int listPosition) {
        return listPosition;
    }

    @Override
    public View getGroupView(int listPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {

        routedata group = (routedata) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_list_group, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.listTitle);
        tv.setText(group.getStopNum()+" "+group.getStopName()+" "+group.getStopLoc());
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }
}

最后像这样设置数据..

for (int i = 0; i < routedataArray.length(); i++) {
                        JSONObject jsonObject, studentobject;
                        JSONArray studentArray;
                        try {
                            jsonObject = routedataArray.getJSONObject(i);


                            ch_list = new ArrayList<studentData>();
                            stopNum = generalFunc.getStringFromJobject(jsonObject, "stopNum");
                            stopName = generalFunc.getStringFromJobject(jsonObject, "stopName");
                            stopLoc = generalFunc.getStringFromJobject(jsonObject, "loc");

                            studentArray = jsonObject.getJSONArray("students");

                            for (int j = 0; j < studentArray.length(); j++) {

                                studentobject = studentArray.getJSONObject(j);
                                studentName = generalFunc.getStringFromJobject(studentobject, "studentName");
                                studentId = generalFunc.getStringFromJobject(studentobject, "studentId");
                                studentData ch = new studentData(studentName,studentId);
                                ch_list.add(ch);

                            }
                            routedata gru = new routedata(stopNum,stopName,stopLoc,ch_list);
                            list.add(gru);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

关于android - 在单个子行中包含多个数据的可扩展 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43584916/

相关文章:

android - 尝试访问其他文件时切换到 Kotlin DSL Unresolved reference

java - 如何将数据库中的图片加载到ListView中?

arrays - 如何用函数中的数据填充数组?

android - 带有 EditText 的 ExpandableListView

android - 能够使用附近连接向多个设备同时发送消息/字节

android - 无法在设备上安装 apk (null)

ajax - Facebook 图谱 API 问题 : How can i get permanent permission/access token to display a public photo album on a webpage?

java - 使用 Jackson 将默认序列化器应用于自定义序列化器中的属性

android - 具有来自各种表的数据的 ExpandableListView

android - monodroid expandablelistview.itemclick 不会被 customview c# 调用