Onscroll selected checkbox getting unchecked using listiview

后端 未结 4 1691
日久生厌
日久生厌 2020-12-02 03:08

In my application i added checkbox with ListView,and i also gave limitation for checkbox,user can not select more than 5 checkbo

4条回答
  •  情话喂你
    2020-12-02 03:46

    Problem is in your getView() method. You set OnCheckedChangeListener and after that you set checkbox to true or false which fires callback in that listener. You should set checkbox check state first and after that set OnCheckedChangeListener.

    Also, that boolean checked[] field is useless, so I simplified your code a little:

     public class CustomAdapter extends BaseAdapter {
        private final LayoutInflater inflater;
        private final Context context;
        private List listData;
    
        public CustomAdapter(Context mainActivity, List listData) {
            context = mainActivity;
            this.listData = listData;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        @Override
        public int getCount() {
            return listData.size();
        }
    
        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
    
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
                holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
                holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.checks.setOnCheckedChangeListener(null);
            holder.checks.setFocusable(false);
    
            if (listData.get(position).isselected) {
                holder.checks.setChecked(true);
            } else {
                holder.checks.setChecked(false);
            }
    
            holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton cb, boolean b) {
    
                    if (checkMaxLimit()) {
    
                        if (listData.get(position).isselected && b) {
                            holder.checks.setChecked(false);
                            listData.get(position).isselected = false;
    
                        } else {
                            holder.checks.setChecked(false);
                            listData.get(position).isselected = false;
                            Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        if (b) {
                            listData.get(position).isselected = true;
                        } else {
                            listData.get(position).isselected = false;
                        }
                    }
                }
            });
    
            holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
            return convertView;
        }
    
        public boolean checkMaxLimit() {
            int countermax = 0;
            for(ModelPooja item : listData){
                if(item.isselected){
                    countermax++;
                }
            }
            return countermax >= 5;
        }
    
        public class ViewHolder {
            TextView tv;
            public CheckBox checks;
        }
    }
    

提交回复
热议问题