ListView View Holder 多次返回重复行

ListView View Holder returning duplicate rows multiple times

这是我的代码,

查看器 Class :

private class ViewHolder {
            TextView tv1;
            TextView tv2;
            TextView tv3;
            TextView tv4;
            TextView tv5;
            TextView tv6;
            TextView tv7;
            TextView tv8;
            TextView tv9;
            TextView tv10;
            TextView tv11;
            TextView tv12;
            TextView tv13;
            TextView tv14;

            EditText edit_qty;
            Button btnbonus;
            Button btnremove;
        }

ListView 自定义适配器 getView() :

@Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            ViewHolder holder = null;
            View view = convertView;
            if(view == null){
                view = lInflater.inflate(R.layout.add_productqty_listitem, parent,
                        false);
                Typeface font = Typeface.createFromAsset(ctx.getAssets(),
                        "gothic.ttf");
                holder = new ViewHolder();
                final ProductInfo product = data.get(position);
                ctx.mProductIdList.add(product.getProductId());
                try {
                    holder.tv1 = (TextView) view
                            .findViewById(R.id.add_product_label);
                    holder.tv1.setText(product.getProductName());
                    holder.tv1.setTypeface(font);
                    holder.tv1.setVisibility(View.VISIBLE);
                    holder.tv2 = (TextView) view
                            .findViewById(R.id.Product_Code_label);
                    holder.tv2.setTypeface(font);
                    holder.tv2.setVisibility(View.VISIBLE);
                    ...............
                    ...............
                    holder.edit_qty.setVisibility(View.VISIBLE);
                    view.setTag(holder);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }else{
                holder = (ViewHolder) view.getTag();
            }
            return view;

        }

不知道viewholder的实现是否正确,ListView有n个重复项,之前已经填充了,谢谢你的帮助

从 getTag 或第一行初始化中获取持有者对象后设置值。所以它应该在 if..else 条件检查之后 在 if.. else 语句

之后检查我在 getview 方法中提到的评论
@Override
            public View getView(final int position, View convertView,
                    ViewGroup parent) {
                ViewHolder holder = null;
                View view = convertView;
                if(view == null){
                    view = lInflater.inflate(R.layout.add_productqty_listitem, parent,
                            false);

                    holder = new ViewHolder();
                try{   Typeface font = Typeface.createFromAsset(ctx.getAssets(),
                            "gothic.ttf");
                        holder.tv1 = (TextView) view
                                .findViewById(R.id.add_product_label);

                        holder.tv1.setTypeface(font);

                        holder.tv2 = (TextView) view
                                .findViewById(R.id.Product_Code_label);
                        holder.tv2.setTypeface(font);

                        view.setTag(holder);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }else{
                    holder = (ViewHolder) view.getTag();
                }


     final ProductInfo product = data.get(position);
                    ctx.mProductIdList.add(product.getProductId());
      // set data , text and visibility option after getting holder from view's getTag()
holder.tv1.setText(product.getProductName());
                        holder.tv1.setVisibility(View.VISIBLE);
                        holder.tv2.setVisibility(View.VISIBLE);
                        ...............
                        ...............
                        holder.edit_qty.setVisibility(View.VISIBLE);
                return view;

我已经更新了你的代码。试试这个。

 @Override
            public View getView(final int position, View convertView,
                    ViewGroup parent) {
                ViewHolder holder = null;
                View view = convertView;
                if(view == null){
                    view = lInflater.inflate(R.layout.add_productqty_listitem, parent,
                            false);
                    holder = new ViewHolder();
                    holder.tv1 = (TextView) view
                                .findViewById(R.id.add_product_label);
                    holder.tv2 = (TextView) view
                                .findViewById(R.id.Product_Code_label);
                    view.setTag(holder);

                }else{
                    holder = (ViewHolder) view.getTag();
                }
                Typeface font = Typeface.createFromAsset(ctx.getAssets(),
                            "gothic.ttf");

                    final ProductInfo product = data.get(position);
                    ctx.mProductIdList.add(product.getProductId());
                    try {

                        holder.tv1.setText(product.getProductName());
                        holder.tv1.setTypeface(font);
                        holder.tv1.setVisibility(View.VISIBLE);

                        holder.tv2.setTypeface(font);
                        holder.tv2.setVisibility(View.VISIBLE);
                        ...............
                        ...............
                        holder.edit_qty.setVisibility(View.VISIBLE);


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                return view;

            }