我有一个复选框,但我想在活动中只保存单个复选框值

时间:2014-04-10 11:15:04

标签: 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) 
      {
           final ViewHolder holder;
           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;

      }          

 }

1 个答案:

答案 0 :(得分:0)

当你选中复选框时,你想在你的班级中检查chnge监听器上实现在字符串中添加值或取消选中你想要的任何地方只需删除那个和当你回到你的视图时你想要先检查值如果匹配则检查该位置的复选框这可能会帮助您

OnCheckedChangeListener Check_Boxes_Listener = new OnCheckedChangeListener(){

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {

        CheckBox cb = (CheckBox) buttonView;

        if (cb.isChecked() == true) {

            if (Maximum_Selected_Check_Boxes < 16) {
                Maximum_Selected_Check_Boxes++;
                HashMap<String, String> Index = new HashMap<String, String>();
                Index.put("Index", cb.getTag().toString());
                selectCBs.add(Index);

            }

            else

            if (Maximum_Selected_Check_Boxes >= 16) {

                Toast.makeText(Check_Boxes,
                        "You have selected the Maximum Items",
                        Toast.LENGTH_SHORT).show();
                cb.setChecked(false);

            }

        } else if (cb.isChecked() == false) {
            Maximum_Selected_Check_Boxes--;

            String Uncheck = cb.getTag().toString();

            int j = 0;
            for (int i = 0; i < selectCBs.size(); i++) {
                if (Uncheck.equalsIgnoreCase(selectCBs.get(i).get("Index")
                        .toString())) {
                    j = i;
                    selectCBs.remove(j);
                    try {
                        Array_of_Update_Query.remove(j);
                    } catch (IndexOutOfBoundsException e) {

                    }

                }

            }

        }

    }

};

check.setOnCheckedChangeListener(Check_Boxes_Listener);

这里有一个关于我正在运行的项目中使用的方法的示例可能是它的帮助