可扩展列表视图子视图问题

问题描述 投票:1回答:2

我正在处理具有多个组布局的可扩展列表视图,但是对于所有布局都有单个子项,但在getChildView()方法中我得到的转换错误无法理解什么是背后的原因请帮助我的朋友

这个错误我得到了

java.lang.ClassCastException:com.app.prominere.standardtakeout.SubItem_ExpandAdapter $ Groupname无法强制转换为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);
    }

}
java android expandablelistview expandablelistadapter
2个回答
1
投票

根据official documentation

不能保证convertView以前是由getChildView(int, int, boolean, View, ViewGroup)创建的

我认为在这种情况下你需要检查

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

0
投票

那是因为你混合了View类型,我想可能来自这里:

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

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

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

您应该为您的孩子使用独特的ID,因为这样他们可以与组ID混合。

© www.soinside.com 2019 - 2024. All rights reserved.