android – 一次只选择一个复选框的自定义列表视图

android – 一次只选择一个复选框的自定义列表视图,第1张

概述我有一个自定义列表视图,每行包含一个复选框和文本.现在我想要的是,如果任何一个listview行的复选框被选中,那么其他行中的其他复选框如果被选中.it将被自动选择.(即一次只能选择一个复选框).我应该怎么做. 到目前为止,我所做的工作如下: public class CustomAdapter extends BaseAdapter{ Context context; List 我有一个自定义列表视图,每行包含一个复选框和文本.现在我想要的是,如果任何一个ListvIEw行的复选框被选中,那么其他行中的其他复选框如果被选中.it将被自动选择.(即一次只能选择一个复选框).我应该怎么做.

到目前为止,我所做的工作如下:

public class CustomAdapter extends BaseAdapter{    Context context;    List<String> items;     boolean array[];    public CustomAdapter(Context context,List<String> items) {    super();    this.context = context;    this.items = items;    array =new boolean[items.size()];}    @OverrIDe    public int getCount() {        // Todo auto-generated method stub        return items.size();    }    @OverrIDe    public Object getItem(int position) {        // Todo auto-generated method stub        return items.get(position);    }    @OverrIDe    public long getItemID(int position) {        // Todo auto-generated method stub        return position;    }    @OverrIDe    public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {        // Todo auto-generated method stub        VIEw v=convertVIEw;        final int pos=position;        if(v==null)        {            v=LayoutInflater.from(context).inflate(R.layout.List,null);        }        TextVIEw txt1=(TextVIEw) v.findVIEwByID(R.ID.textVIEw1);        final CheckBox chkBox=(CheckBox) v.findVIEwByID(R.ID.checkBox1);        txt1.setText(items.get(position));        int selectedindexitem=0;        chkBox.setonClickListener(new OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                // Todo auto-generated method stub                if(chkBox.isChecked())                {                    array[pos]=true;                }else{                    array[pos]=false;                }            }        });         chkBox.setChecked(array[pos]);        return v;     }}In this code i can select multiple checkBox at a time but i need only one checkBox should be checked one at a time.
解决方法 尝试更改所有项目布尔值false在通知适配器后排除选定项目,并为ListVIEw性能实现 ViewHolder设计模式:
@OverrIDe    public VIEw getVIEw(final int position,VIEwGroup parent) {        VIEwHolder holder;        if(convertVIEw==null){            holder = new VIEwHolder();            convertVIEw = LayoutInflater.from(context).inflate(R.layout.List,null);            holder.txt1 = (TextVIEw) convertVIEw.findVIEwByID(R.ID.textVIEw1);            holder.chkBox = (CheckBox) convertVIEw.findVIEwByID(R.ID.checkBox1);            convertVIEw.setTag(holder);        }else{            holder = (VIEwHolder) convertVIEw.getTag();        }        holder.txt1.setText(items.get(position));        holder.chkBox.setChecked(array[position]);        holder.chkBox.setonClickListener(new OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                for (int i=0;i<array.length;i++){                    if(i==position){                        array[i]=true;                    }else{                        array[i]=false;                    }                }                notifyDataSetChanged();            }        });        return convertVIEw;    }    class VIEwHolder{        TextVIEw txt1;        CheckBox chkBox;    }
总结

以上是内存溢出为你收集整理的android – 一次只选择一个复选框的自定义列表视图全部内容,希望文章能够帮你解决android – 一次只选择一个复选框的自定义列表视图所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存