【问题标题】:android editText inside expandableList wrong index and wrong count?expandableList中的android editText错误索引和错误计数?
【发布时间】:2017-10-02 23:02:34
【问题描述】:

根据要编辑的editText的结果,我在Expandable List中得到了editText作为childcount的数量。但是editText位置总是不同或childcount错误,我得到这个错误:

("java.lang.IndexOutOfBoundsException: 无效索引 3,大小为 3")

我的 ExpandableListAdapter:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    public ExpandableListView expandableListView;
    private ChildViewHolder childViewHolder;
    private GroupViewHolder groupViewHolder;
    private Context _context;
    private ArrayList<BBQrValidateModel.Campaigns> mDataCampaignModel; //stampcount and totalneed 


    public ExpandableListAdapter(ArrayList<BBQrValidateModel.Campaigns> mDataCampaignModel,
                                 Context context, ExpandableListView expandableListView) {
        this._context = context;
        this.mDataCampaignModel = mDataCampaignModel;
        this.expandableListView = expandableListView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
    return campaign.coffeeGiftCount;
    }

    @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) {
        BBQrValidateModel.Campaigns campaign = mDataCampaignModel.get(groupPosition);

        if (convertView == null) {

            convertView = MainActivity.inflater.inflate(R.layout.row_campaign_buy_or_hide, parent, false);

            childViewHolder = new ChildViewHolder();

            childViewHolder.txtCoffeeGift = convertView.findViewById(R.id.txtCoffeeGift);
            childViewHolder.txtCoffeeHide = convertView.findViewById(R.id.txtCoffeeHide);
            childViewHolder.txtCoffeeUse = convertView.findViewById(R.id.txtCoffeeUse);


            convertView.setTag(childViewHolder);

        } else {

            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        childViewHolder.txtCoffeeGift.setText(campaign.prize);
        //kullan ve sakla tıklanınca modeldekı degısıklıgı buradan yapıcaz

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        BBQrValidateModel.Campaigns campaign = mDataCampaignModel.get(groupPosition);

        return campaign.coffeeGiftCount;
    }

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

    @Override
    public int getGroupCount() {
        return (mDataCampaignModel == null) ? 0 : mDataCampaignModel.size();
    }

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

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        final BBQrValidateModel.Campaigns campaign = mDataCampaignModel.get(groupPosition);

        if (convertView == null) {
            convertView = MainActivity.inflater.inflate(R.layout.row_campaigndetail, parent, false);

            groupViewHolder = new GroupViewHolder();
            groupViewHolder.txtStampCount = convertView.findViewById(R.id.txtStampCount);
            groupViewHolder.txtCampaignName = convertView.findViewById(R.id.txtCampaignName);
            groupViewHolder.txtTotalNeeds = convertView.findViewById(R.id.txtTotalNeeds);
            groupViewHolder.btnFreepProductNum1 = convertView.findViewById(R.id.btnFreepProductNum1);
            groupViewHolder.btnFreepProductNum2 = convertView.findViewById(R.id.btnFreepProductNum2);
            groupViewHolder.btnFreepProductNum3 = convertView.findViewById(R.id.btnFreepProductNum3);
            groupViewHolder.btnFreepProductNum4 = convertView.findViewById(R.id.btnFreepProductNum4);
            groupViewHolder.btnFreepProductNum5 = convertView.findViewById(R.id.btnFreepProductNum5);
            groupViewHolder.edtPurchasedCoffeeCount = convertView.findViewById(R.id.edtPurchasedCoffeeCount);

            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        groupViewHolder.edtPurchasedCoffeeCount.setId(groupPosition);
        groupViewHolder.txtTotalNeeds.setText(String.valueOf(campaign.totalNeeds));
        groupViewHolder.txtStampCount.setText(String.valueOf(campaign.stampCount));
        groupViewHolder.txtCampaignName.setText(campaign.description);

        if (groupViewHolder.edtPurchasedCoffeeCount.getText().toString().isEmpty()) {
            campaign.edtPurchasedCoffeeCount = 0;
        } else {
            campaign.edtPurchasedCoffeeCount = Integer.valueOf(groupViewHolder.edtPurchasedCoffeeCount
                    .getText().toString());
        }

    final int totalneeds = campaign.totalNeeds;
    final int stampcount = campaign.stampCount;

    campaign.coffeeGiftCount = (stampcount + campaign.edtPurchasedCoffeeCount) / totalneeds;

    TextWatcher inputTextWatcher = new TextWatcher()
    {
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {

            if (stampcount + campaign.coffeeGiftCount > totalneeds)
            {

                campaign.coffeeGiftCount = (stampcount + campaign.edtPurchasedCoffeeCount) / totalneeds;

            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
        }

        public void afterTextChanged(Editable s)
        {
            notifyDataSetChanged();

        }
    };
    groupViewHolder.edtPurchasedCoffeeCount.addTextChangedListener(inputTextWatcher);
    expandableListView.expandGroup(groupPosition);
    return convertView;
    }

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

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

我的 ViewHolders(适配器中的内部类):

    public final class GroupViewHolder {

        TextView txtStampCount;
        TextView txtCampaignName;
        TextView txtTotalNeeds;
        EditText edtPurchasedCoffeeCount;

        Button btnFreepProductNum1;
        Button btnFreepProductNum2;
        Button btnFreepProductNum3;
        Button btnFreepProductNum4;
        Button btnFreepProductNum5;
    }

    public final class ChildViewHolder {
        TextView txtCoffeeGift;
        TextView txtCoffeeUse;
        TextView txtCoffeeHide;
    }

}

【问题讨论】:

    标签: android android-edittext expandablelistview expandablelistadapter android-textwatcher


    【解决方案1】:

    在 getChild 方法中,您返回 0 表示您没有该子对象的任何子对象,将数据分组到您应该返回该位置的子对象的位置。

    public class ExpandableListAdapter extends BaseExpandableListAdapter {
    
        private Context _context;
        private List<String> _listDataHeader; // header titles
        // child data in format of header title, child title
        private HashMap<String, List<String>> _listDataChild;
    
        public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                     HashMap<String, List<String>> listChildData) {
            this._context = context;
            this._listDataHeader = listDataHeader;
            this._listDataChild = listChildData;
        }
    
        @Override
        public Object getChild(int groupPosition, int childPosititon) {
            return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                    .get(childPosititon);
        }
    
        @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 String childText = (String) getChild(groupPosition, childPosition);
    
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_item, null);
            }
    
            EditText edtListChild = (EditText) convertView
                    .findViewById(R.id.lblListItem);
    
            txtListChild.setText(childText);
            return convertView;
        }
    
        @Override
        public int getChildrenCount(int groupPosition) {
            return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                    .size();
        }
    
        @Override
        public Object getGroup(int groupPosition) {
            return this._listDataHeader.get(groupPosition);
        }
    
        @Override
        public int getGroupCount() {
            return this._listDataHeader.size();
        }
    
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
    
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            String headerTitle = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_group, null);
            }
    
            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.lblListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle);
    
            return convertView;
        }
    
        @Override
        public boolean hasStableIds() {
            return false;
        }
    
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
    

    这段代码对你有帮助,对我有帮助。

    【讨论】:

    • 我像你说的那样改变了我的代码,但 childcount 没有更新
    • i.hizliresim.com/Gy21aZ.jpglike this childcount ==>(edttext+0)/totalneed
    【解决方案2】:

    在您的代码中更改以下方法:

     @Override
        public Object getChild(int groupPosition, int childPosititon)
        {
            //return 0;
            Return value of child here
    
        }
    
     @Override
        public Object getGroup(int groupPosition)
        {
            //return 0;
            Return value of group here
        }
    

    【讨论】:

    • 我改变了,但edittext没有更新,childcount也没有更新
    • i.hizliresim.com/Gy21aZ.jpglike this childcount ==>(edttext+0)/totalneed
    • 你能解释一下你的问题吗
    • 我想划分edittext数量和总需求,最多划分的数量希望看到孩子的数量。但是当我尝试不同的解决方案时,edittext的位置正在改变i.hizliresim.com/Gy21aZ.jpg
    • campaign.coffeeGiftCount = (stampcount + campaign.edtPurchasedCoffeeCount) / totalneeds;我想查看campaign.coffeeGiftCount 的子编号
    猜你喜欢
    • 2017-08-27
    • 2017-05-04
    • 2019-04-03
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 2011-06-12
    相关资源
    最近更新 更多