ListView View Holder returning duplicate rows multiple times

ぐ巨炮叔叔 提交于 2019-12-02 19:24:26

问题


Here is my code,

View Holder 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 Custom Adapters 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;

        }

I dont know whether the implement of view holder is correct or not , ListView has n number of duplicates which has been populated earlier , your help will be thankfull


回答1:


Set the values once you get the holder object from getTag or first row initlization. so it should after if.. else condition checking Check the comment also i hve mentioned in getview Method after if.. else statment

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



回答2:


I have updated your code. Try this.

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

            }


来源:https://stackoverflow.com/questions/28105826/listview-view-holder-returning-duplicate-rows-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!