来自JSON的3级可消耗列表视图

时间:2017-11-10 03:58:01

标签: android json expandablelistview expandablelistadapter

我想从json数据创建3级可扩展列表视图。

在My JSON中,有3个级别相互嵌套。

这是我的适配器,它只能包含前2级 在这个适配器的帮助下,我成功解析了2级,但我在3级别中遇到了问题。

请建议我解决方案。提前致谢

这是我的json格式

 {
    "status": 1,
    "category": [
        {
            "id": 4,
            "category_name_en": "Car",
            "category_name_ar": "Car",
            "description_en": "Car",
            "description_ar": "Car",
            "day": "monday,tuesday,wednesday,thursday,friday,saturday,sunday",
            "status": "A",
            "image_path": "http://kartpay.biz/file/images-category/no-image.png",
            "created_at": "Oct 16 2017 11:25 AM",
            "updated_at": "Oct 16 2017 11:25 AM",
            "sub_category": [
                {
                    "id": 9,
                    "sub_category_name_en": "American",
                    "sub_category_name_ar": "American",
                    "sub_description_en": "American",
                    "sub_description_ar": "American",
                    "status": "A",
                    "image_path": "http://kartpay.biz/file/images-subcategory/no-image.png",
                    "created_at": "Oct 16 2017 11:25 AM",
                    "updated_at": "Oct 16 2017 11:25 AM",
                    "child_category": [
                        {
                            "id": 3,
                            "child_category_name_en": "GMC",
                            "child_category_name_ar": "GMC",
                            "child_description_en": "GMC",
                            "child_description_ar": "GMC",
                            "status": "A",
                            "image_path": "http://kartpay.biz/file/images-childcategory/detailed/3/gSdANeI9tu0CeFzqflkR3rCaS387JbGt8m4dpXf0.png",
                            "created_at": "Oct 25 2017 8:06 AM",
                            "updated_at": "Oct 25 2017 8:06 AM"
                        },





public class TerbaruAdapter extends BaseExpandableListAdapter{
    Context context;
    ArrayList<TerbaruModel>ListTerbaru;
    ArrayList<ArrayList<ChildTerbaru>> ListChildTerbaru;
    int count;

    public TerbaruAdapter (Context context, ArrayList<TerbaruModel>ListTerbaru,ArrayList<ArrayList<ChildTerbaru>> ListChildTerbaru){
        this.context=context;
        this.ListTerbaru=ListTerbaru;
        this.ListChildTerbaru=ListChildTerbaru;
//      this.count=ListTerbaru.size();
//      this.count=ListChildTerbaru.size();
    }
    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }


    @Override
    public ChildTerbaru getChild(int groupPosition, int childPosition) {


        return ListChildTerbaru.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {

        ChildTerbaru childTerbaru = getChild(groupPosition, childPosition);
        ViewHolder holder= null;

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.expandablelistview_child, null);

            holder=new ViewHolder();
            holder.begdate1=(TextView)convertView.findViewById(R.id.tv_listchilds);
           // holder.enddate1=(TextView)convertView.findViewById(R.id.end_date);
            convertView.setTag(holder);

        }
        else{
            holder=(ViewHolder)convertView.getTag();
        }

        holder.begdate1.setText(childTerbaru.getSub_category_name_en());
       // holder.enddate1.setText(childTerbaru.getEndDate());

        return convertView;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
        return ListChildTerbaru.get(groupPosition).size();
    }

    @Override
    public TerbaruModel getGroup(int groupPosition) {
        return ListTerbaru.get(groupPosition);
    }

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

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

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

        TerbaruModel terbaruModel = (TerbaruModel) getGroup(groupPosition);
        ViewHolder holder= null;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.expandablelistview_group, null);

            holder=new ViewHolder();
            holder.nama=(TextView)convertView.findViewById(R.id.tv_listtitle);
           // holder.alamat=(TextView)convertView.findViewById(R.id.address);
            convertView.setTag(holder);

        }

        else{
            holder=(ViewHolder)convertView.getTag();
        }

        holder.nama.setText(terbaruModel.getCat_name());
       // holder.alamat.setText(terbaruModel.getAlamat());

        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }


    static class ViewHolder{
        TextView begdate1, enddate1,nama, alamat, imageid;
    }

}

0 个答案:

没有答案