ListView视图持有者多次返回重复的行

时间:2015-01-23 08:31:36

标签: android listview android-listview android-viewholder

这是我的代码,

查看持有人类:

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;

        }

我不知道视图持有者的实现是否正确,ListView有多个先前填充过的重复项,感谢您的帮助

2 个答案:

答案 0 :(得分:2)

我已更新您的代码。试试这个。

 @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;

            }

答案 1 :(得分:1)

从getTag或第一行initlization获取holder对象后设置值。所以它应该在if .. else条件检查之后 检查我在if.. else statment

之后的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;