Populate ExpandableListView from 2 different Models. gives me a IndexOutOfBoundsException

50 Views Asked by At

I am trying to populate an ExpandableListView with data from a json API using retrofit but whenever i try it it gives me IndexOutOfBoundsException i made sure the models are right the spelling everything but i can't get to a solution and for clarity this is my first time using expandablelistview i understand its concept please view my code below.

JSON Response

{
  "Status": 200,
  "Message": "OK",
  "Data": {
    "SubService": [
      {
        "Id": 13,
        "MainServiceId": 11,
        "SubServiceNameAr": "سبلت",
        "SubServiceNameEn": "Split AC",
        "ServicePriceLists": [
          {
            "Id": 18,
            "SubServiceId": 13,
            "DescriptionAr": "تنظيف مكيف سبليت\r\n",
            "DescriptionEn": "Cleaning Split AC\r\n",
            "Price": 120,
            "Cost": 100,
            "Tasks": null
          },
          {
            "Id": 19,
            "SubServiceId": 13,
            "DescriptionAr": "تعبئة غاز فريون\r\n",
            "DescriptionEn": "Refilling Freon\r\n",
            "Price": 150,
            "Cost": 120,
            "Tasks": null
          },
          {
            "Id": 20,
            "SubServiceId": 13,
            "DescriptionAr": "تسرب مياه المكيف",
            "DescriptionEn": "Water Leakage\r\n",
            "Price": 70,
            "Cost": 50,
            "Tasks": null
          }
        ]
      },
      {
        "Id": 14,
        "MainServiceId": 11,
        "SubServiceNameAr": "عامودى/قائم",
        "SubServiceNameEn": "Vertical/Horizontal AC",
        "ServicePriceLists": []
      },
      {
        "Id": 15,
        "MainServiceId": 11,
        "SubServiceNameAr": "شباك\r\n",
        "SubServiceNameEn": "Window AC\r\n",
        "ServicePriceLists": []
      },
      {
        "Id": 16,
        "MainServiceId": 11,
        "SubServiceNameAr": "كاسيت\r\n",
        "SubServiceNameEn": "Cassette AC\r\n",
        "ServicePriceLists": []
      },
      {
        "Id": 17,
        "MainServiceId": 11,
        "SubServiceNameAr": "مخفى\r\n",
        "SubServiceNameEn": "Hidden AC\r\n",
        "ServicePriceLists": []
      },
      {
        "Id": 18,
        "MainServiceId": 11,
        "SubServiceNameAr": "وحدة كاملة",
        "SubServiceNameEn": "Full Unit AC\r\n",
        "ServicePriceLists": []
      },
      {
        "Id": 19,
        "MainServiceId": 12,
        "SubServiceNameAr": "الانارة\r\n",
        "SubServiceNameEn": "Lighting\r\n",
        "ServicePriceLists": [
          {
            "Id": 23,
            "SubServiceId": 19,
            "DescriptionAr": "تركيب ثريا صغيرة\r\n",
            "DescriptionEn": "Small Chandelier installation\r\n",
            "Price": 50,
            "Cost": 35,
            "Tasks": null
          },
          {
            "Id": 24,
            "SubServiceId": 19,
            "DescriptionAr": "تركيب ضوء خارجى\r\n",
            "DescriptionEn": "External Light installation\r\n",
            "Price": 80,
            "Cost": 60,
            "Tasks": null
          },
          {
            "Id": 26,
            "SubServiceId": 19,
            "DescriptionAr": "تركيب سبوت لاين\r\n",
            "DescriptionEn": "Spot light Installation\r\n",
            "Price": 65,
            "Cost": 50,
            "Tasks": null
          },
          {
            "Id": 27,
            "SubServiceId": 19,
            "DescriptionAr": "تغيير لمبة عادية او ليد\r\n",
            "DescriptionEn": "Changing Normal/LED Lamps\r\n",
            "Price": 45,
            "Cost": 30,
            "Tasks": null
          },
          {
            "Id": 28,
            "SubServiceId": 19,
            "DescriptionAr": "تركيب قاعدة لمبة عادية او ليد\r\n",
            "DescriptionEn": "Installing Normal/LED lamp base\r\n",
            "Price": 50,
            "Cost": 30,
            "Tasks": null
          },
          {
            "Id": 29,
            "SubServiceId": 19,
            "DescriptionAr": "تركيب سبوت لايت كبير\r\n",
            "DescriptionEn": "Big Spot Light installation\r\n",
            "Price": 90,
            "Cost": 65,
            "Tasks": null
          }
        ]
      }
    ]
  }
}

SubServiceAdapter(ExpandableListViewAdapter)

public class SubServiceAdapter extends BaseExpandableListAdapter{

    private Context mContext;
    private List<SubServiceModel> parentItemArrayList;
    private List<ServicePriceListModel> childItemArrayList;
    private TextView tv_parentItem, tv_childItem;
//    private ImageView iv_groupIndicator;
//    private HashMap<String, List<ServicePriceListModel>> servicePriceListModels;


    public SubServiceAdapter(Context mContext, List<SubServiceModel> parentItemArrayList) {
        this.mContext = mContext;
        this.parentItemArrayList = parentItemArrayList;
//        this.servicePriceListModels = servicePriceListModels;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        childItemArrayList = parentItemArrayList.get(groupPosition).getServicePriceListsModel();
        return childItemArrayList.get(childPosition);
    }

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

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup viewGroup) {
        ServicePriceListModel childItemInfo = (ServicePriceListModel) getChild(groupPosition,childPosition);
        if(view == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.expandable_listview_child,null);
        }

        tv_childItem = (TextView)view.findViewById(R.id.ServicePriceDesc);
        tv_childItem.setText(childItemInfo.getDescriptionAr());

        return view;
    }

    @Override
    public int getChildrenCount(int i) {
        childItemArrayList = parentItemArrayList.get(i).getServicePriceListsModel();
        return childItemArrayList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parentItemArrayList.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        SubServiceModel parentItemInfo = (SubServiceModel) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.mContext.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_listview_parent, null);
        }
        TextView listTitleTextView = (TextView) convertView
                .findViewById(R.id.SubServiceName);
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(parentItemInfo.getSubServiceNameAr());
        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }}

MainActivity

public class MainActivity extends AppCompatActivity {

    private HashMap<String, SubServiceModel> subjects;
    private List<SubServiceModel> parentItemsList;
    private List<ServicePriceListModel> childItemsList;
    private SubServiceAdapter customAdapter;
    private ExpandableListView elvSimple;
    private ImageView iv_groupIndicator;
    private int previousGroup = -1;
    SubServiceViewModel subServiceViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeView();
        loadData();
        collapseAll();
    }

    public void initializeView() {
        subjects = new HashMap<>();
        parentItemsList = new ArrayList<>();
        childItemsList = new ArrayList<>();
        customAdapter = new SubServiceAdapter(MainActivity.this, parentItemsList);
        elvSimple = findViewById(R.id.ELVSubService);
        elvSimple.setAdapter(customAdapter);

//        iv_groupIndicator = findViewById(R.id.iv_groupIndicator);

        elvSimple.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

                if (elvSimple.isGroupExpanded(groupPosition)) {
                    elvSimple.collapseGroup(groupPosition);
                    previousGroup = -1;
                } else {
                    elvSimple.expandGroup(groupPosition);
                    if (previousGroup != -1) {
                        elvSimple.collapseGroup(previousGroup);
                    }
                    previousGroup = groupPosition;
                }

                return true;
            }
        });

        elvSimple.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                /* Auto Scrolling */
                elvSimple.setSelectedGroup(groupPosition);
            }
        });
    }

    private void loadData() {
//        addItem();
        addItem(parentItemsList.get(1).getSubServiceNameAr(), parentItemsList.get(1).getServicePriceListsModel().get(1).getDescriptionAr());
//        addItem(parentItemsList.get(1).getSubServiceNameAr(), childItemsList.get(1).getDescriptionAr());
        
    }

    private int addItem(String parentItemName, String childItemName) {
        int groupPosition = 0;

        //check the hash map if the group already exists
        SubServiceModel parentItemObj = subjects.get(parentItemName);

        //add the group if doesn't exists
        if (parentItemObj == null) {
            parentItemObj = new SubServiceModel();
            parentItemObj.setSubServiceNameAr(parentItemName);
            subjects.put(parentItemName, parentItemObj);

            parentItemsList.add(parentItemObj);

        }

        //get the children for the group
        childItemsList = parentItemObj.getServicePriceListsModel();

        //size of the children list
        int listSize = childItemsList.size();

        //add to the counter
        listSize++;

        //create a new child and add that to the group

        ServicePriceListModel childItemObj = new ServicePriceListModel();
        childItemObj.setDescriptionAr(childItemName);
        childItemsList.add(childItemObj);
        parentItemObj.setServicePriceListsModel(childItemsList);

        //find the group position inside the list
        groupPosition = parentItemsList.indexOf(parentItemObj);
        return groupPosition;
    }

    //method to expand all groups
    private void expandAll() {
        int count = customAdapter.getGroupCount();
        for (int i = 0; i < count; i++) {
            elvSimple.expandGroup(i);
        }
    }

    //method to collapse all groups
    private void collapseAll() {
        int count = customAdapter.getGroupCount();
        for (int i = 0; i < count; i++) {
            elvSimple.collapseGroup(i);
        }
    }
}

If anything else is needed plz don't hesitate to ask me any help would be appreciated thanks in advance.

0

There are 0 best solutions below