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

前端之家收集整理的这篇文章主要介绍了android – 一次只选择一个复选框的自定义列表视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个自定义列表视图,每行包含一个复选框和文本.现在我想要的是,如果任何一个listview行的复选框被选中,那么其他行中的其他复选框如果被选中.it将被自动选择.(即一次只能选择一个复选框).我应该怎么做.

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

  1. public class CustomAdapter extends BaseAdapter{
  2.  
  3. Context context;
  4. List<String> items;
  5.  
  6. boolean array[];
  7.  
  8.  
  9. public CustomAdapter(Context context,List<String> items) {
  10. super();
  11. this.context = context;
  12. this.items = items;
  13. array =new boolean[items.size()];
  14. }
  15.  
  16.  
  17. @Override
  18. public int getCount() {
  19. // TODO Auto-generated method stub
  20. return items.size();
  21. }
  22.  
  23. @Override
  24. public Object getItem(int position) {
  25. // TODO Auto-generated method stub
  26. return items.get(position);
  27. }
  28.  
  29. @Override
  30. public long getItemId(int position) {
  31. // TODO Auto-generated method stub
  32. return position;
  33. }
  34.  
  35. @Override
  36. public View getView(int position,View convertView,ViewGroup parent) {
  37. // TODO Auto-generated method stub
  38. View v=convertView;
  39. final int pos=position;
  40. if(v==null)
  41. {
  42. v=LayoutInflater.from(context).inflate(R.layout.list,null);
  43. }
  44.  
  45. TextView txt1=(TextView) v.findViewById(R.id.textView1);
  46. final CheckBox chkBox=(CheckBox) v.findViewById(R.id.checkBox1);
  47.  
  48. txt1.setText(items.get(position));
  49. int selectedindexitem=0;
  50. chkBox.setOnClickListener(new OnClickListener() {
  51.  
  52. @Override
  53. public void onClick(View v) {
  54. // TODO Auto-generated method stub
  55. if(chkBox.isChecked())
  56. {
  57. array[pos]=true;
  58.  
  59.  
  60. }else{
  61. array[pos]=false;
  62. }
  63. }
  64. });
  65. chkBox.setChecked(array[pos]);
  66.  
  67.  
  68.  
  69.  
  70.  
  71. return v;
  72. }
  73.  
  74.  
  75.  
  76.  
  77. }
  78.  
  79. 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设计模式:
  1. @Override
  2. public View getView(final int position,ViewGroup parent) {
  3. ViewHolder holder;
  4. if(convertView==null){
  5. holder = new ViewHolder();
  6. convertView = LayoutInflater.from(context).inflate(R.layout.list,null);
  7. holder.txt1 = (TextView) convertView.findViewById(R.id.textView1);
  8. holder.chkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
  9. convertView.setTag(holder);
  10. }else{
  11. holder = (ViewHolder) convertView.getTag();
  12. }
  13.  
  14. holder.txt1.setText(items.get(position));
  15. holder.chkBox.setChecked(array[position]);
  16. holder.chkBox.setOnClickListener(new OnClickListener() {
  17.  
  18. @Override
  19. public void onClick(View v) {
  20. for (int i=0;i<array.length;i++){
  21. if(i==position){
  22. array[i]=true;
  23. }else{
  24. array[i]=false;
  25. }
  26. }
  27. notifyDataSetChanged();
  28. }
  29. });
  30.  
  31. return convertView;
  32. }
  33.  
  34. class ViewHolder{
  35. TextView txt1;
  36. CheckBox chkBox;
  37. }

猜你在找的Android相关文章