到目前为止,我所做的工作如下:
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 – 一次只选择一个复选框的自定义列表视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)