如何在android中使用单选复选框?

时间:2014-04-04 05:22:07

标签: android

public class ImageAdapter extends BaseAdapter 
 {
      private LayoutInflater mInflater;
      private Context mContext;
      public ImageAdapter(Context context) 
      {
           mContext = context;
      }
      public int getCount() 
      {
           return count;
      }
      public Object getItem(int position) 
      {
           return position;
      }
      public long getItemId(int position) 
      {
           return position;
      }
      public View getView(final int position, View convertView, ViewGroup parent) 
      {
           ViewHolder holder;
           if (convertView == null) 
           {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.state_info, null);

                holder.textview = (TextView) convertView.findViewById(R.id.thumbImage);                  

                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);                   
                convertView.setTag(holder);

           } 
           else 
           {
                holder = (ViewHolder) convertView.getTag();
           }          
           holder.checkbox.setId(position);
           holder.textview.setId(position);

           holder.checkbox.setOnClickListener(new OnClickListener() {
                public void onClick(View v) 
                {       
                    // String h="hello";
                     CheckBox cb = (CheckBox) v;
                     int id = cb.getId();

                     if(cb.isChecked())
                     {
                         cb.setChecked(false);
                     }                        
                     Toast.makeText(Four.this, "Selected CheckBox ID" + v.getId(), Toast.LENGTH_SHORT).show();

                }
           });

           holder.textview.setText(items.get(position).getName());

           holder.checkbox.setChecked(thumbnailsselection[position]);
           holder.id = position;
           return convertView;
      }          
      public void clear()
      {
          CheckBox cb=(CheckBox)findViewById(R.id.itemCheckBox);
        //  int id=cb.getId();
          for(int j=0;j<count;j++)
          {              
                  cb.setChecked(false);
          }
      }
 }

 class ViewHolder 
 {
      TextView textview , textIndexId;
      CheckBox checkbox;
      int id;
 }

2 个答案:

答案 0 :(得分:0)

您可以做一件事而不是使用复选框,您可以使用氡气按钮,但只需将样式更改为复选框

答案 1 :(得分:0)

public class ImageAdapter扩展了BaseAdapter      {           private LayoutInflater mInflater;           private Context mContext;           public ImageAdapter(Context context)           {                mContext = context;           }           public int getCount()           {                返回计数;           }           public Object getItem(int position)           {                返回位置;           }           public long getItemId(int position)           {                返回位置;           }           public View getView(final int position,View convertView,ViewGroup parent)           {                ViewHolder持有人;                if(convertView == null)                {
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.state_info,null);

                holder = new ViewHolder();                                      
                holder.textview = (TextView) convertView.findViewById(R.id.thumbImage);                                              
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox); 

                convertView.setTag(holder);

           } 
           else 
           {    
                holder = (ViewHolder) convertView.getTag();
           }          

           convertView.setTag(holder);

           //holder.checkbox.setId(position);
           holder.textview.setId(position);

           holder.checkbox.setId(position);
           holder.checkbox.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) 
               {
                   for (int i = 0; i < count; i++) 
                   {
                       if (v.getId() == i) 
                       {
                           thumbnailsselection[i] = true;
                           Log.v("check", ""+position);

                       } 
                       else 
                       {
                           thumbnailsselection[i] = false;
                       }
                   }
                   notifyDataSetChanged();
               }
           });

           if (thumbnailsselection[position]) 
           {
               holder.checkbox.setChecked(true);
           } 
           else 
           {
               holder.checkbox.setChecked(false);
           }

           holder.textview.setText(items.get(position).getName());

           holder.checkbox.setChecked(thumbnailsselection[position]);
           holder.id = position;
           return convertView;

      }          

 }