Search code examples
javaandroidexpandablelistviewexpandablelistadapter

Expandale list view's Child view's issue


I am working on the expandable list view with multiple group layouts but having single child as common for all the layouts but in the getChildView() method I am getting the cast error not able understand whats the reason behind please help me out friends

this error I am getting

java.lang.ClassCastException: com.app.prominere.standardtakeout.SubItem_ExpandAdapter$Groupname cannot be cast to com.app.prominere.standardtakeout.SubItem_ExpandAdapter$Childnames

BaseAdapter.Java

public class SubItem_ExpandAdapter extends BaseExpandableListAdapter {
    Context subcontext;
    private ArrayList<Subitem_base> subitem_bases;
    private SharedPreferences loginPreferences;
    private SharedPreferences.Editor loginPrefsEditor;
    String itembasename, itemde, subtmsub, subitempri;
    private Activity parentActivity;
    public static final int single = 1;
    public static final int two = 2;

    public SubItem_ExpandAdapter(Context subcontext, ArrayList<Subitem_base> subitem_bases, Activity parentactivity) {
        this.subcontext = subcontext;
        this.subitem_bases = subitem_bases;
        this.parentActivity = parentactivity;
    }

    static class Groupname {
        private TextView group_name;
        private TextView groupitem_price;
//        private Button grouporder;
    }

    static class Childnames {
        private TextView item_name;
        private TextView item_price;
        private Button order;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<Subitem_base_child> subitem_base_children = subitem_bases.get(groupPosition).getItems();
        return subitem_base_children.size();
    }


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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<Subitem_base_child> subitem_base_children = subitem_bases.get(groupPosition).getItems();
        return subitem_base_children.get(childPosition);
    }

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

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

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

    @Override
    public int getGroupTypeCount() {
        return 2;
    }

    @Override
    public int getGroupType(int groupPosition) {
        Subitem_base groupstype = subitem_bases.get(groupPosition);
        if (groupstype.getGrpcount().equals("1")) {
            return single;
        } else {
            return two;
        }
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        Groupname groupname;
        Subitem_base groupitem = subitem_bases.get(groupPosition);
        int grouptype = getGroupType(groupPosition);
        if (convertView == null) {
            if (grouptype == single) {
                convertView = LayoutInflater.from(subcontext).inflate(R.layout.groupcount_single, parent, false);
            } else {
                convertView = LayoutInflater.from(subcontext).inflate(R.layout.group_item, parent, false);
            }
            groupname = new Groupname();
            groupname.group_name = (TextView) convertView.findViewById(R.id.group_name);
            groupname.groupitem_price = (TextView) convertView.findViewById(R.id.groupitem_price);
//            groupname.grouporder = (Button) convertView.findViewById(R.id.grouporder);
            convertView.setTag(groupname);
        } else {
            groupname = (Groupname) convertView.getTag();
        }

        groupname.group_name.setText(groupitem.getGroupitemname());
        groupname.groupitem_price.setText(groupitem.getGrprice());
        if (isExpanded) {
            String[] elements = {groupitem.getGroupitemname()};
            for (String s : elements) {
                itembasename = s;
            }
        }
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final Childnames childname;
        if (convertView == null) {
            convertView = LayoutInflater.from(subcontext).inflate(R.layout.child_item, parent, false);
            childname = new Childnames();
            childname.item_name = (TextView) convertView.findViewById(R.id.item_name);
            childname.item_price = (TextView) convertView.findViewById(R.id.item_price);
            childname.order = (Button) convertView.findViewById(R.id.order);
            convertView.setTag(childname);
        } else {
            childname = (Childnames) convertView.getTag();
        }
        Subitem_base_child childitem = (Subitem_base_child) getChild(groupPosition, childPosition);
        if (childitem.getChilditemname().isEmpty()) {
            childname.item_name.setVisibility(View.GONE);
        } else {
            childname.item_name.setText(childitem.getChilditemname());
        }
        childname.item_price.setText(childitem.getChilditemprice());
        loginPreferences = subcontext.getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
        final String status = loginPreferences.getString("Status", "");
        if (status.equals("0")) {
            childname.order.setBackgroundResource(R.drawable.cart_disable);
        } else {
            childname.order.setBackgroundResource(R.drawable.cart_button);
        }

        childname.order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (status.equals("0")) {
                    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(parentActivity);
                    SharedPreferences customerid = subcontext.getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
                    String opentimes = customerid.getString("open", "");
                    alertDialogBuilder.setMessage(opentimes);
                    alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            alertDialogBuilder.setCancelable(true);
                        }
                    });
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();
                } else {
                    String poundre = childname.item_price.getText().toString();
                    subtmsub = childname.item_name.getText().toString();
                    subitempri = poundre.replace("£", "");
                    addcart();
                }
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private void addcart() {
        loginPreferences = subcontext.getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
        loginPrefsEditor = loginPreferences.edit();
        SharedPreferences customerid = subcontext.getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
        loginPreferences.edit();
        String customid = customerid.getString("customerid", "");
        String cartdetails = itembasename + " " + subtmsub;
        String cartspacong = cartdetails.replace(" ", "$");
        cartspacong = cartspacong.replace("&", "and");
        String addcarturl = Constant.commonurltake + "cart_process.php?userid=" + customid + "&Item=" + cartspacong + "&Itemcount=1&price=" + subitempri + "&page=items";
        Log.d("Cart", addcarturl);
        JsonArrayRequest cartreq = new JsonArrayRequest(Request.Method.POST, addcarturl, (String) null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject cartobj = response.getJSONObject(i);
                        String cartnumber = cartobj.getString("count");
                        loginPrefsEditor.putString("Count", cartnumber);
                        BaseActivity.cartnumber.setVisibility(View.VISIBLE);
                        BaseActivity.cartnumber.setText(cartnumber);
                        loginPrefsEditor.apply();
                        if (cartobj.has("message")) {
                            Toast carttost = Toast.makeText(subcontext, cartobj.getString("message"), Toast.LENGTH_SHORT);
                            carttost.setGravity(Gravity.CENTER, 0, 0);
                            carttost.show();
                        } else {
                            Toast carttost = Toast.makeText(subcontext, "Product Added To Cart", Toast.LENGTH_SHORT);
                            carttost.setGravity(Gravity.CENTER, 0, 0);
                            carttost.show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(subcontext, "" + error, Toast.LENGTH_SHORT).show();
            }
        });
        cartreq.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(cartreq);
    }

}

Solution

  • According to official documentation

    It is not guaranteed that the convertView will have been previously created by getChildView(int, int, boolean, View, ViewGroup)

    I think in this case you need to check

    if (convertView != null && convertView.getTag() instance of Childnames) {
      // reuse the convertView
    else
      // create new view
    }