具有单个chlild行中的多个数据的可扩展列表视图

时间:2017-04-24 09:51:13

标签: android json expandablelistview expandablelistadapter

我想显示我引用此链接http://www.journaldev.com/9942/android-expandablelistview-example-tutorial的组和子数据,但教程在子视图和父视图中只有单项,我想在父视图和子视图中设置多个项目。任何人都可以帮我这样做吗?

Configure::write('Session', array(
'defaults' => 'cache',//default session
'timeout'  => '<time_in_minute>',//in minutes
'cookieTimeout' => '<time_in_minute>',//in minutes
'ini' => array('session.cookie_domain' => env('HTTP_BASE')),
'handler' => array(
    'config' => '<handler>' //if you are using default sessions then this field is not necessary
 )
));

我的json数据是,

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

我解析过这个,

"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": []
    }
  ]

1 个答案:

答案 0 :(得分:1)

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

import Foundation
import UIKit

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
label.numberOfLines = 1
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
label.text = "Some very long text goes here"

studentData.java

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;
    }
}

像这样改变适配器..

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;
    }
}