1

I want to create 3 level expandable listview from json data.

In My JSON there are 3 level which are nested under each other.

This is my Adapter which able to contain only first 2 level With the help of this adapter i am successfully parse upto 2 level but i am getting problem in 3 level category .

Please suggest me solution . Thanks in advance

This is my json format

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

}
Harshal Deshmukh
  • 1,787
  • 3
  • 14
  • 25
  • I had same problem long time ago. I solved using dropdown for first level, then used expandablelistview for 2nd and 3rd level. It worked for me! :) – Dee Nix Nov 10 '17 at 06:08
  • Possible duplicate of [How to display more than 3- levels of expandable List View?](https://stackoverflow.com/questions/18765638/how-to-display-more-than-3-levels-of-expandable-list-view) – akhilesh0707 Nov 10 '17 at 06:23
  • can you send me example @Deepak Ojha – Harshal Deshmukh Nov 10 '17 at 10:01
  • I can tell you steps. You need to organize your json data. Take a `Hashmap` variable and store first level data as a key and corresponding (2nd and 3rd) data as a value. Display key in dropdown and value in `ExpandableListview`. When dropdown value change change elistview data also. This l[ink](https://github.com/ri3tykity/CustomExpandable-Listview-android) will help a little. – Dee Nix Nov 10 '17 at 15:53

0 Answers0