Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中使用单选复选框?_Android - Fatal编程技术网

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

如何在android中使用单选复选框?,android,Android,您可以做一件事,而不是使用复选框您可以使用radon按钮,但只需将样式更改为复选框公共类ImageAdapter extends BaseAdapter { 私人停车场; 私有上下文; 公共图像适配器上下文上下文 { mContext=上下文; } 公共整数getCount { 返回计数; } 公共对象getItemint位置 { 返回位置; } 公共长GetItemId位置 { 返回位置; } 公共视图getViewfinal int位置、视图转换视图、视图组父视图 { 视窗座; 如果conv

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

公共类ImageAdapter extends BaseAdapter { 私人停车场; 私有上下文; 公共图像适配器上下文上下文 { mContext=上下文; } 公共整数getCount { 返回计数; } 公共对象getItemint位置 { 返回位置; } 公共长GetItemId位置 { 返回位置; } 公共视图getViewfinal int位置、视图转换视图、视图组父视图 { 视窗座; 如果convertView==null { convertView=LayoutFlater.frommContext.inflateR.layout.state_info,空

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;
 }

看起来,他正在用自定义布局项填充ListView,因此创建了ViewHolderPattern,每个都包含一些文本和一个复选框。现在一次只能检查其中一个视图。
                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;

      }          

 }