Search code examples
androidlistviewexpandablelistviewindexoutofboundsexception

ExpandableListview getting IndexOutOfBoundsException if group's childeren counts are Zero


Hope you all are doing good. Well, I have been wasting almost 2 days(48 hours) in a bit problem. I am working with expandablelistview some of my list's group have children and rest are empty have no children. The list is going well when it has children but if the group have no children the app is crashed.

Moreover what I am doing is here bellow.

 public void getCartData(){
    cartProgress.setVisibility(View.VISIBLE);
    listDataHeader = new ArrayList<>();
    ListChild = new ArrayList<>();
    DatabaseReference query = mDatabase;
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //child is each element in the finished list
             td = (HashMap<String, Object>) dataSnapshot.getValue();
            if(td!=null) {

                values = td.values();
                String string = values.toString();

                JSONArray jsonArray = null;
                try {
                    jsonArray = new JSONArray(values);

                    for (int a = 0; a < jsonArray.length(); a++) {

                        JSONObject allJsonObject = jsonArray.getJSONObject(a);

                        CartFragParentModel cartFragParentModel = new CartFragParentModel();

                        cartFragParentModel.setItem_name(allJsonObject.optString("mName"));
                        cartFragParentModel.setItem_price(allJsonObject.optString("mPrice"));
                        mQuantity = allJsonObject.optString("mQuantity");
                        cartFragParentModel.setItem_quantity(allJsonObject.optString("mQuantity"));
                        cartFragParentModel.setItem_symbol(allJsonObject.optString("mCurrency"));

                        listDataHeader.add(cartFragParentModel);
                        listChildData = new ArrayList<>();

                        if (!allJsonObject.has("extraItem")) {
                            cartProgress.setVisibility(View.GONE);
                        }
                        else {
                            JSONArray extraItemArray = allJsonObject.getJSONArray("extraItem");

                            for (int b = 0; b < extraItemArray.length(); b++) {

                                JSONObject jsonObject = extraItemArray.getJSONObject(b);

                                CartFragChildModel cartFragChildModel = new CartFragChildModel();

                                cartFragChildModel.setQuantity(allJsonObject.optString("mQuantity"));
                                cartFragChildModel.setSymbol(allJsonObject.optString("mCurrency"));
                                cartFragChildModel.setName(jsonObject.optString("menu_extra_item_name"));
                                cartFragChildModel.setPrice(jsonObject.optString("menu_extra_item_price"));

                                listChildData.add(cartFragChildModel);
                            }
                            ListChild.add(listChildData);

                        }

                        cartFragExpandable = new CartFragExpandable(getContext(), listDataHeader, ListChild);
                        selected_item_list.setAdapter(cartFragExpandable);
                        cartProgress.setVisibility(View.GONE);
                        int itemCount = cartFragExpandable.getGroupCount();

                        for(int i=0; i < cartFragExpandable.getGroupCount(); i++)
                                    try {
                                        selected_item_list.expandGroup(i);
                                    }catch (IndexOutOfBoundsException e){
                                        e.getCause();
                                    }




                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            else {
                cartProgress.setVisibility(View.GONE);
                no_cart_div.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

Also where I get exception that is custom adapter

ublic class CartFragExpandable  extends BaseExpandableListAdapter {

Context context;
ArrayList<CartFragParentModel> ListTerbaru;
ArrayList<ArrayList<CartFragChildModel>> ListChildTerbaru;

public CartFragExpandable (Context context, ArrayList<CartFragParentModel>ListTerbaru, ArrayList<ArrayList<CartFragChildModel>> ListChildTerbaru){
    this.context=context;
    this.ListTerbaru=ListTerbaru;
    this.ListChildTerbaru=ListChildTerbaru;

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


@Override
public CartFragChildModel 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, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    final CartFragChildModel childTerbaru = getChild(groupPosition, childPosition);

    CartFragExpandable.ViewHolder holder= null;
    notifyDataSetChanged();

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

        holder=new CartFragExpandable.ViewHolder();
        holder.item_detail_tv = (TextView)convertView.findViewById(R.id.item_detail_tv);

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

    String quantity = childTerbaru.getQuantity();
    String name = childTerbaru.getName();
    String price = childTerbaru.getPrice();
    String symbol = childTerbaru.getSymbol();
    holder.item_detail_tv.setText(quantity+"x "+name+" + "+symbol+price);

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

}

@Override
public CartFragParentModel 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) {


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

     /*   ExpandableListView mExpandableListView = (ExpandableListView) parent;
        mExpandableListView.expandGroup(groupPosition);*/

        holder=new CartFragExpandable.ViewHolder();
        holder.name_tv=(TextView)convertView.findViewById(R.id.name_tv);
        holder.price_tv = (TextView)convertView.findViewById(R.id.price_tv);

        convertView.setTag(holder);

    }

    else{
        holder=(CartFragExpandable.ViewHolder)convertView.getTag();
    }
    String quantity = terbaruModel.getItem_quantity();
    String name = terbaruModel.getItem_name();
    String price = terbaruModel.getItem_price();
    String symbol = terbaruModel.getItem_symbol();

    holder.name_tv.setText(name+" x"+quantity);
    holder.price_tv.setText(symbol+price);

    return convertView;
}

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

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

static class ViewHolder{
    TextView name_tv,price_tv,item_detail_tv;
}

}

Please suggest me solution, really appreciate to you.


Solution

  • This will help.

    For empty child update child with the empty list not null. Add this line to Activity class:

     if (!allJsonObject.has("extraItem")) { 
           cartProgress.setVisibility(View.GONE); 
           ListChild.add(listChildData); 
      } 
    

    And then return child count

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

    Try this while return child count.