Android 解决在ListView历史复用中数据显示混乱

时间:2022-04-10 09:14:32

Android 解决在ListView历史复用中数据显示混乱

如图所示,我们做了个提交评价功能,而UI设计让我们逐个评论,商量后还是要保持这样,那我们就只能按照这样的实现,那么这就用一个listview列表显示,item里面有好评差评textview,自定义星星控件,EditText,然后这样做了,中间出了问题,数据有四条,根据长度显示,只能显示下两条,那么当我操作了第一条数据的时候,比如我点击了差评,选择了星星,或填写了文本,当我滑动listview,滑动到最后的时候,我操作的第一条这三个控件,它显示的一模一样,最后查资料,原来是listview复用的原因,故此记录以免下次再犯错.

 class Evaluater_Listview_Adapter extends BaseAdapter {
        private Context content;
        private List<EvaluateVo.DataBean> arrayList;

        public Evaluater_Listview_Adapter(Context content, List<EvaluateVo.DataBean> arrayList) {
            this.content = content;
            this.arrayList = arrayList;
        }

        public void setData(List<EvaluateVo.DataBean> arrayList) {
            this.arrayList = arrayList;
        }

        @Override
        public int getCount() {
            return this.arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {


            final Evaluater_ViewHolder viewHolder;

            if (convertView == null) {
                convertView = LayoutInflater.from(content).inflate(R.layout.transaction_complete_order_item_layout, null);
                viewHolder = new Evaluater_ViewHolder(convertView);
                viewHolder.etContent.setTag(evaluateVo.getData());

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (Evaluater_ViewHolder) convertView.getTag();
                viewHolder.etContent.setTag(position);
            }
            viewHolder.rlJf.setVisibility(View.GONE);
            viewHolder.llPj.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(arrayList.get(position).getImage(), viewHolder.userPendingPaymentGoodsImg);//图片
            viewHolder.WaitPaymentOrderProductName.setText(arrayList.get(position).getName());//名字
            viewHolder.WaitPaymentOrderGoodsPrice.setText("¥ " + arrayList.get(position).getPrice());//金额

            **//上面代码就和一般的adapter一样从这里开始才是关键代码,我用一个实体类来存储这三个控件的点击或者输入状态,然后根据position绑定位置settag**

            final EvaluateVo.DataBean bean = arrayList.get(position);
            viewHolder.etContent.setTag(bean);
            viewHolder.srvRatable.setTag(bean);
            viewHolder.textHaop.setTag(bean);
            viewHolder.textChap.setTag(bean);
             **//清除焦点**
            viewHolder.etContent.clearFocus();


            viewHolder.etContent.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    **//获得Edittext所在position里面的Bean,并设置数据**

                    EvaluateVo.DataBean bean = (EvaluateVo.DataBean) viewHolder.etContent.getTag();
                    bean.setInput(s + "");
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });

            **//判断根据position判断,看是否为空为空的那个就表明没有进行操作的.就设置为""有数据的就设置它显示的**

            if (!TextUtils.isEmpty(bean.getInput())) {
                viewHolder.etContent.setText(bean.getInput());
            } else {
                viewHolder.etContent.setText("");
            }

 **//判断根据position判断,看是否为00的那个就表明没有进行操作的.就设置为0有数据的就设置它显示的星星数量**

            if (bean.getStart() != 0) {
                viewHolder.srvRatable.setRate(bean.getStart());
            } else {
                viewHolder.srvRatable.setRate(0);
            }
             **//判断根据position判断,看是否为11的那个就表明进行操作的.就设置textview改变颜色**
            if (bean.getIsClick() == 1) {
                viewHolder.textHaop.setTextColor(getResources().getColor(R.color.black_666));
                viewHolder.textChap.setTextColor(getResources().getColor(R.color.orange_ed));
            } else {
                viewHolder.textHaop.setTextColor(getResources().getColor(R.color.orange_ed));
                viewHolder.textChap.setTextColor(getResources().getColor(R.color.black_666));
            }

            if (!TextUtils.isEmpty(bean.getPraise())) {
                if (bean.getPraise().equals("0")) {
                    viewHolder.textHaop.setTextColor(getResources().getColor(R.color.black_666));
                    viewHolder.textChap.setTextColor(getResources().getColor(R.color.orange_ed));
                } else {
                    viewHolder.textHaop.setTextColor(getResources().getColor(R.color.orange_ed));
                    viewHolder.textChap.setTextColor(getResources().getColor(R.color.black_666));
                }
            }
            **//绑定点击好评进行改变颜色**
            viewHolder.textHaop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    praise = 1;
                    EvaluateVo.DataBean bean = (EvaluateVo.DataBean) viewHolder.textHaop.getTag();
                    bean.setIsClick(1);
                    viewHolder.textHaop.setTextColor(getResources().getColor(R.color.orange_ed));
                    viewHolder.textChap.setTextColor(getResources().getColor(R.color.black_666));
                }
            });
            **//绑定点击差评进行改变颜色**
            viewHolder.textChap.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    praise = 2;
                    bean.setPraise("0");
                    EvaluateVo.DataBean bean = (EvaluateVo.DataBean) viewHolder.textChap.getTag();
                    bean.setIsClick(2);
                    viewHolder.textHaop.setTextColor(getResources().getColor(R.color.black_666));
                    viewHolder.textChap.setTextColor(getResources().getColor(R.color.orange_ed));
                }
            });
             **//绑定点击星星进行改星星个数**

            viewHolder.srvRatable.setOnRateChangeListener(new StarRatingView.OnRateChangeListener() {
                @Override
                public void onRateChange(int rate) {
                    EvaluateVo.DataBean bean = (EvaluateVo.DataBean) viewHolder.srvRatable.getTag();
                    bean.setStart(rate);
                }
            });
            List<EvaluateVo.DataBean.SkuDataArrBean> list = null;
            for (int i = 0; i < arrayList.size(); i++) {
                list = arrayList.get(i).getSku_data_arr();
            }

//
//            viewHolder.textShuxing.setText("商品属性 :" + list.get(position).getValue());
            return convertView;
        }

        class Evaluater_ViewHolder {
            @BindView(R.id.user_pending_payment_goods_img)
            ImageView userPendingPaymentGoodsImg;
            @BindView(R.id.WaitPaymentOrder_product_name)
            TextView WaitPaymentOrderProductName;
            @BindView(R.id.WaitPaymentOrder_goods_price)
            TextView WaitPaymentOrderGoodsPrice;
            @BindView(R.id.name_price_layout)
            RelativeLayout namePriceLayout;
            @BindView(R.id.WaitPaymentOrder_product_num)
            TextView WaitPaymentOrderProductNum;
            @BindView(R.id.donate_text)
            TextView donateText;
            @BindView(R.id.WaitPaymentOrder_donate_num)
            TextView WaitPaymentOrderDonateNum;
            @BindView(R.id.rl_jf)
            RelativeLayout rlJf;
            @BindView(R.id.text_sqtk)
            TextView textSqtk;
            @BindView(R.id.text_sqwq)
            TextView textSqwq;
            @BindView(R.id.text_tkjd)
            TextView textTkjd;
            @BindView(R.id.text_wqjd)
            TextView textWqjd;
            @BindView(R.id.text_shuxing)
            TextView textShuxing;
            @BindView(R.id.rl_ddxq)
            RelativeLayout rlDdxq;
            @BindView(R.id.text_haop)
            TextView textHaop;
            @BindView(R.id.text_chap)
            TextView textChap;
            @BindView(R.id.srv_ratable)
            StarRatingView srvRatable;
            @BindView(R.id.et_content)
            EditText etContent;
            @BindView(R.id.ll_pj)
            LinearLayout llPj;

            Evaluater_ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }