Android列表视图在滚动时更改复选框值

时间:2015-06-26 06:32:02

标签: android listview checkbox scroll

我遇到一个奇怪的问题,我有一个Listview,每行包含checkbox。当checkboxcheckedunchecked时,我正在更新edittext之外listview的值,形成来自textview的相应数值。但我面临的问题是,当我滚动listview我已检查的checkboxes成为unchecked时,edittext之外的listview值也会相应更新我的自定义adapter如下所示:

public class ListAdapter extends BaseAdapter {
        Context ctx;
        LayoutInflater lInflater;
        ArrayList<LItem> lstItems;
        TextView tvAQuantity , tvBQuantity;
        EditText etQuantity ;
        LItem p;
        ViewHolder holder;

        ListAdapter(Context context, ArrayList<LItem> objects) {
            ctx = context;
            lstItems = objects;
           //  lInflater = (LayoutInflater) ctx .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

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

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

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

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

            if (view == null) {
                lInflater = (LayoutInflater) ctx .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = lInflater.inflate(R.layout.list_style_task
                        , parent, false);


                holder = new ViewHolder();

                holder.tvAQuan = (TextView) view.findViewById(R.id.tvAQuantity);
                holder.tvBQuan=(TextView)view.findViewById(R.id.tvBQuantity);
                holder.etQuan=(EditText)view.findViewById(R.id.etTaskQuantity);


                holder.tvMatN= (TextView) view.findViewById(R.id.tvMaterial )   ;
                holder.tvTksN= (TextView) view.findViewById(R.id.tvTask )   ;
                holder.chkSelect=(CheckBox)view.findViewById(R.id.chkSelected);
                holder.btnRefreshQuantity=(Button)view.findViewById(R.id.btnRefreshQuantity);
                //SelectQuantity=(EditText)findViewById(R.id.etSQuantity);
                view.setTag(holder);

                //  tvAQuantity.setText(p.getAQ());
                //   etQuantity.setTag(position);

            }
            else {

                holder=(ViewHolder)view.getTag();
               }
            p = getProduct(position);
            if(p!=null) {
                if (holder.tvAQuan != null) {


                    holder.tvAQuan.setText(p.getAQ());
                    holder.tvBQuan.setText(p.getBQ());
                    holder.tvMatN.setText(p.getMName());
                    holder.tvTksN.setText(p.getTName());

                    holder.chkSelect.setChecked(p.getSelected());


                }
            }

        final int     posi=position;

            holder.btnRefreshQuantity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RelativeLayout  rl = (RelativeLayout)v.getParent();

                    holder.tvAQuan = (TextView) rl .findViewById(R.id.tvAQuantity);
                    holder.tvBQuan = (TextView) rl .findViewById(R.id.tvBQuantity);
                    holder.etQuan=(EditText)rl.findViewById(R.id.etTaskQuantity);

                    if(Float.parseFloat (holder.etQuan.getText().toString())<=Float.parseFloat (holder.tvBQuan.getText().toString()))
                    {
                        p = getProduct(posi);
                        p.setAQ(holder.etQuan.getText().toString());
                        String s = holder.etQuan.getText().toString();
                        notifyDataSetChanged();
                    }
                    else
                    {
                        Toast.makeText(myactivity,"Quantity should be less than balance quantity!",Toast.LENGTH_SHORT).show();

                    }
                }
            });



          holder.chkSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked)
                    {
                        p = getProduct(position);
                        p.setSelected(true);
                        float total=Float.parseFloat (etSelectedQuantity.getText().toString())+Float.parseFloat(p.getAQ().toString());
                        etSelectedQuantity.setText(Float.toString(total));



                    }
                    else
                    {

                        p = getProduct(position);
                        p.setSelected(false);
                        float  total=Float.parseFloat (etSelectedQuantity.getText().toString())-Float.parseFloat(p.getAQ().toString());
                        etSelectedQuantity.setText(Float.toString(total));

                    }
                }
            });




            //  CheckBox cbBuy = (CheckBox) view.findViewById(R.id.checkbox);
            //cbBuy.setOnCheckedChangeListener(myCheckChangList);
            //  cbBuy.setTag(position);
            //  cbBuy.setChecked(p.selected);
            return view;
        }

        LItem getProduct(int position)
        {
            return ((LItem) getItem(position));
        }

        ArrayList<LItem> getBox() {
            ArrayList<LItem> box = new ArrayList<LItem>();
            for (LItem p : lstItems) {
                //  if (p.selected)
                //  box.add(p);
            }
            return box;
        }




        private   class ViewHolder{

            public TextView tvAQuan;
            public TextView tvBQuan;
            public EditText  etQuan;
            public TextView tvMatN;
            public TextView tvTksN;
            public CheckBox chkSelect;
            public Button btnRefreshQuantity;

        }

    }

1 个答案:

答案 0 :(得分:0)

试试这个,

         if (view == null) {
            ......
          }
          else {
            .......
          }
        holder.chkSelect.setOnCheckedChangeListener(null);// you need to set listener to null.
            p = getProduct(position);
            if(p!=null) {
                if (holder.tvAQuan != null) {    
                    holder.tvAQuan.setText(p.getAQ());
                    holder.tvBQuan.setText(p.getBQ());
                    holder.tvMatN.setText(p.getMName());
                    holder.tvTksN.setText(p.getTName());

                    holder.chkSelect.setChecked(p.getSelected());
                }
            }
    // place remaining code...