【问题标题】:I have a multiple checkbox but i want save only single checkbox value in activity [closed]我有一个多个复选框,但我只想在活动中保存单个复选框值[关闭]
【发布时间】:2014-04-10 22:29:08
【问题描述】:

这是我的代码:

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;

      }          

 }

【问题讨论】:

  • 查看 sharedPreferences 并在其中保存一个 bool 值,在 onResume 中读取它并设置复选框的检查状态
  • 您的问题是什么?请说的不仅仅是代码。
  • 这里的问题是什么?将复选框值保存在哪里?在 SQLite 中,在服务器上,在文件中,在活动中.. ???
  • @user2294439 伙计,没人会吃你的,请解释一下你到底在做什么
  • 如何在活动中保存复选框值。

标签: android


【解决方案1】:

当复选框被选中时,您希望在您的类中实现 on check chnge listener 添加字符串中的值或任何您想要取消选中的位置,只需将其删除 当您返回视图时,您想首先检查值如果匹配,则选中该位置的复选框这可能会对您有所帮助

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

这是我正在运行的项目中使用的方法的示例,可能会对您有所帮助

【讨论】:

  • 你能不能给我提供代码解决方案。
  • 根据您的需要更改方法,只需在检查更改侦听器上调用它们
  • 这个变量的类型 selectCBs.add(Index); @Hasnain Ali
  • 这是一个数组列表,我在我的代码中使用你想要的不需要这个数组列表你需要根据你的需要修改这个方法
猜你喜欢
  • 2020-05-12
  • 2013-12-25
  • 1970-01-01
  • 2011-06-19
  • 2017-09-10
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多