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

android–Onscroll选中的复选框使用listiview取消选中,第1张

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

在我的应用程序中我添加了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 – Onscroll选中的复选框使用listiview取消选中全部内容,希望文章能够帮你解决android – Onscroll选中的复选框使用listiview取消选中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1102817.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存