ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – Onscroll选中的复选框使用listiview取消选中

2019-09-25 20:34:57  阅读:260  来源: 互联网

标签:android-checkbox android listview baseadapter


在我的应用程序中我添加了ListView复选框,我也给了复选框的限制,用户不能选择超过5复选框,但问题是滚动我选中的复选框取消选中以下是我的代码片段,任何人都可以帮我这个

public class CustomAdapter extends BaseAdapter {

    private LayoutInflater inflater = null;
    Context context;

    String rup = "\u20B9";
    private ArrayList<ModelPooja> listData;
    boolean checked[];

    public CustomAdapterPooja(Context mainActivity, ArrayList<ModelPooja> listData) {
        // TODO Auto-generated constructor stub

        context = mainActivity;
        this.listData = listData;
        inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        checked = new boolean[listData.size()];
        for (int i = 0; i < checked.length; i++) {
            checked[i] = listData.get(i).isselected;
        }

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         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.serviceprice = (TextView) convertView.findViewById(R.id.list_item_poojaprice);
        holder.dayss = (TextView) convertView.findViewById(R.id.list_item_poojadays);
        holder.txtseledates = (TextView) convertView.findViewById(R.id.selecteddatess);
        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);

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

            }
        });
        if (listData.get(position).isselected()) {
            holder.checks.setChecked(true);
        } else {
            holder.checks.setChecked(false);
        }

        holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
        holder.dayss.setText(listData.get(position).getPOOJA_LISTING_DAYS());
        holder.serviceprice.setText(rup + listData.get(position).getPOOJA_LISTING_AMOUNT());

        return convertView;
    }

    public boolean checkMaxLimit() {
        int countermax = 0;
        for (int i = 0; i < checked.length; i++) {
            checked[i] = false;
            checked[i] = listData.get(i).isselected();
            if (listData.get(i).isselected()) {
                countermax++;
            }
        }
        return countermax >= 5 ? true : false;
    }

    public class ViewHolder {
        TextView tv;
        TextView serviceprice;
        public CheckBox checks;
        public TextView dayss;
        public TextView txtseledates;
    }


}

解决方法:

问题出在你的getView()方法中.您设置OnCheckedChangeListener,然后将checkbox设置为true或false,从而触发该侦听器中的回调.您应首先设置复选框检查状态,然后设置OnCheckedChangeListener.

此外,布尔检查[]字段是无用的,所以我简化了你的代码:

 public class CustomAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private final Context context;
    private List<ModelPooja> listData;

    public CustomAdapter(Context mainActivity, List<ModelPooja> 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;
    }
}

标签:android-checkbox,android,listview,baseadapter
来源: https://codeday.me/bug/20190925/1816813.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有