【问题标题】:How to select all when i check mark All CheckBox?当我选中所有复选框时如何全选?
【发布时间】:2016-10-01 16:51:47
【问题描述】:

这张图片显示了我的listview 的样子

当我们选中 All 时,所有的复选框都应该被选中,当我们取消选中时,所有的复选框都应该被取消选中,这里是 ListView All 也是列表视图的一部分,它在地图列表中的位置是 0...

public class MultiSelectBaseAdapter extends BaseAdapter{

    Context context;
    LayoutInflater inflater;
    List<Boolean> selectedList;
    Map<Integer, MultipleSelect> mapList;

    public MultiSelectBaseAdapter(Context context, Map<Integer, MultipleSelect> mapList) {
        this.context = context;
        this.mapList = mapList;
        selectedList = new ArrayList<>();
        for(int i=0 ; i< mapList.size() ; i++){
            selectedList.add(i, mapList.get(i).isSelected());
        }

        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public List<Boolean> getSelected(){
        return selectedList;
    }

    public Map<Integer, MultipleSelect> getMapList(){
        return mapList;
    }

    @Override
    public int getCount() {
        if(mapList != null){
            return mapList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mapList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.multiselect_item_layout, parent, false);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.textview_multiselect);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox_multiselect);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.checkBox.setTag(position);

        holder.textView.setText(mapList.get(position).getKeyValueType().getName());

        if(selectedList.get(position)){
            holder.checkBox.setChecked(true);
        } else{
            holder.checkBox.setChecked(false);
        }

        holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int index = (Integer)buttonView.getTag();
                if(isChecked){
                    selectedList.set(index, true);
                }else{
                    selectedList.set(index, false);
                }
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        TextView textView;
        CheckBox checkBox;
    }

}

请帮助我,我是 android 新手,我没有使用 CheckBox

[在此处输入图片描述][2]

【问题讨论】:

    标签: android list checkbox


    【解决方案1】:

    在您的 selectedList 中添加所有选定的项目 id,然后调用适配器的 notifyDataSetChanged() 方法

    【讨论】:

      【解决方案2】:

      将布尔值添加到您的 MultipleSelect 模型并在您选中或取消选中时更改它,并在 onBindViewHolder 中显示时检查此值并设置复选框状态。

      要全选,请将地图中每个项目的此值更改为 true 并调用:

      yourMultiselectAdapter.notifyDataSetChanged();
      

      取消全选也一样——true 及以上调用的值)

      【讨论】:

        【解决方案3】:

        我做了这样的事情。 这是 y 类侧代码 SelectAll =(Button)findViewById(R.id.footerapproveselectall); SelectAll.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
        
                            String btnlbl = SelectAll.getText().toString();
                            if(btnlbl.equalsIgnoreCase("SelectAll"))
                            {
                                // TODO Auto-generated method stub
                                CustomListAdapter adapter=new CustomListAdapter(Approver.this,AttendanceModelList,1);
                                lv1.setAdapter(adapter);
                                SelectAll.setText("Uncheck All");
        
        
                            }else
                            {
                                // TODO Auto-generated method stub
                                CustomListAdapter adapter=new              CustomListAdapter(Approver.this, AttendanceModelList,2);
                                lv1.setAdapter(adapter);
                                SelectAll.setText("SelectAll");
        
                            }
        
                        }
                    });  
        

        这是我的适配器端代码 公共类 CustomListAdapter 扩展 ArrayAdapter{

        private final Activity context;
        /*  private final List<String> empname;
            private final List<String> reason;
            private final List<String> empno;
            private final List<String> empdate;
            private final List<String> empDay ;
            private final List<String> empaid;
        */
        private Dialog dialog;
        private Dialog dialog1;
        private Dialog dialogz;
        private AlertDialog BackDialog;
        int cheki;
        Long id;
        String Givenregon;
        
        
        boolean checkAll_flag = false;
        boolean checkItem_flag = false;
        private final List<AttendanceModel> AttendanceModelList  ;
        
        private TextView NameShow;
        private TextView NoShow;
        private TextView DayShow;
        private TextView DateShow;
        private TextView ReasonShow;
        private Button Approve;
        private Button Cansel;
        private Button Reject;
        //Date date;
        public CustomListAdapter(Activity context, List<AttendanceModel> AttendanceModelList,int cheki)
        {
            super(context, R.layout.mylist,AttendanceModelList);
            // TODO Auto-generated constructor stub
        
            this.context=context;
            this.AttendanceModelList =AttendanceModelList;
            this.cheki =cheki;
        
        }
        
        static class ViewHolder {
            protected TextView txtTitle;
            protected TextView textemployeedate;
            protected ImageButton View1;
            protected TextView textempday;
            protected  CheckBox Chek1   ;
        
        }
        
        public View getView(int position,View convertview,ViewGroup parent) {
            ViewHolder viewHolder = null;
        
            if (convertview == null){
                LayoutInflater inflater=context.getLayoutInflater();
                convertview=inflater.inflate(R.layout.mylist, null);
                viewHolder = new ViewHolder();
                viewHolder.txtTitle = (TextView) convertview.findViewById(R.id.idemployeename);
                //TextView extratxt = (TextView) rowView.findViewById(R.id.idreason);
                //TextView txemployee = (TextView) rowView.findViewById(R.id.idempno);
                viewHolder. textemployeedate = (TextView)convertview.findViewById(R.id.idempdate);
                viewHolder. textempday = (TextView) convertview.findViewById(R.id.idempday);
        /*  Button Submit = (Button) rowView.findViewById(R.id.btapprove);
         *
         */
            /*Button Cansel = (Button) rowView.findViewById(R.id.btreject);*/
                viewHolder. View1 =(ImageButton)convertview.findViewById(R.id.btmylistviewshow);
        
                viewHolder. Chek1 = (CheckBox)convertview.findViewById(R.id.checkmylist);
        
                viewHolder.Chek1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                        AttendanceModelList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                    }
                });
                convertview.setTag(viewHolder);
                convertview.setTag(R.id.idemployeename,viewHolder.txtTitle);
                convertview.setTag(R.id.idempday,viewHolder.textempday);
                convertview.setTag(R.id.idempdate,viewHolder.textemployeedate);
                convertview.setTag(R.id.checkmylist,viewHolder.Chek1);
            }
            else {
                viewHolder = (ViewHolder) convertview.getTag();
            }
        
        
            viewHolder.Chek1.setTag(position);
            viewHolder.Chek1.setChecked(AttendanceModelList.get(position).isSelected());
            viewHolder.txtTitle.setText(AttendanceModelList.get(position).getEmpname());
        
        
        
        
        
        
            if(cheki==1)
            {
                viewHolder. Chek1.setChecked(true);
            }
            else if(cheki==2)
            {
                viewHolder. Chek1.setChecked(false);
            }
        
        
            return convertview;
        
        
        
        };
        

        【讨论】:

          【解决方案4】:

          MainActivity.java

          import android.os.Bundle;
          import android.support.v7.app.AppCompatActivity;
          import android.support.v7.widget.Toolbar;
          import android.widget.ArrayAdapter;
          import android.widget.ListView;
          
          public class MainActivity extends AppCompatActivity {
          
              Toolbar toolbar;
              ListView listView;
              String Name[]={"All","One","Two","Three","Four","Five"};
              boolean Some[]={false,false,false,false,false,false};
              MyAdapter adapter;
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  toolbar = (Toolbar) findViewById(R.id.toolbar);
                  setSupportActionBar(toolbar);
                  listView = (ListView) findViewById(R.id.listView);
                  adapter = new MyAdapter(MainActivity.this,Name,Some);
                  listView.setAdapter(adapter);
          
          
          
              }
          
              public void myDAta1(int pos)
              {
                   Some[pos]=false;
                  adapter.notifyDataSetChanged();
          
              }
          
              public void myDAta2(int pos)
              {
                  Some[pos]=true;
                  adapter.notifyDataSetChanged();
          
              }
          
              public void myDAta3()
              {
          
                  for(int i=0;i<Name.length;i++)
                  {
                      Some[i]=false;
                  }
                  adapter.notifyDataSetChanged();
          
              }
          
              public void myDAta4()
              {
          
                  for(int i=0;i<Name.length;i++)
                  {
                      Some[i]=true;
                  }
                  adapter.notifyDataSetChanged();
          
              }
          
          
          }
          

          我的适配器

          import android.content.Context;
          import android.view.LayoutInflater;
          import android.view.View;
          import android.view.ViewGroup;
          import android.widget.BaseAdapter;
          import android.widget.CheckBox;
          import android.widget.TextView;
          
          
          public class MyAdapter extends BaseAdapter {
              String Name[];
              boolean Some[];
              Context context;
              LayoutInflater inflater;
              MyAdapter(Context mContext,String mName[],boolean mSome[])
              {
                this.context=mContext;
                this.Name= mName;
                this.Some=mSome;
              }
          
              @Override
              public int getCount() {
                  return Name.length;
              }
          
              @Override
              public Object getItem(int position) {
                  return Name[position];
              }
          
              @Override
              public long getItemId(int position) {
                  return position;
              }
          
              @Override
              public View getView(final int position, View convertView, ViewGroup parent)
              {
                  final ViewHolder holder ;
                  if (convertView == null) {
                      inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                      convertView = inflater.inflate(R.layout.row_layout, parent, false);
                      holder = new ViewHolder();
                      holder.textView = (TextView) convertView.findViewById(R.id.textView);
                      holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
                      convertView.setTag(holder);
                  } else {
                      holder = (ViewHolder) convertView.getTag();
                  }
                  holder.textView.setText(Name[position]);
                  holder.checkBox.setChecked(Some[position]);
          
          
                  if(position==0) {
                      if (holder.checkBox.isChecked()) {
                          holder.checkBox.setOnClickListener(new View.OnClickListener() {
                              @Override
                              public void onClick(View v) {
                                  if (context instanceof MainActivity) {
                                      ((MainActivity) context).myDAta3();
                                  }
                              }
                          });
                      } else {
                          holder.checkBox.setOnClickListener(new View.OnClickListener() {
                              @Override
                              public void onClick(View v) {
                                  if (context instanceof MainActivity) {
                                      ((MainActivity) context).myDAta4();
                                  }
                              }
                          });
                      }
          
                  }
                  else
                  {
                      if(holder.checkBox.isChecked())
                      {
                          holder.checkBox.setOnClickListener(new View.OnClickListener() {
                              @Override
                              public void onClick(View v) {
                                  if(context instanceof MainActivity){
                                      ((MainActivity)context).myDAta1(position);
                                  }
                              }
                          });
                      }
                      else
                      {
                          holder.checkBox.setOnClickListener(new View.OnClickListener() {
                              @Override
                              public void onClick(View v) {
                                  if(context instanceof MainActivity){
                                      ((MainActivity)context).myDAta2(position);
                                  }
                              }
                          });
                      }
                  }
                  return convertView;
              }
              public static class ViewHolder {
                  TextView textView;
                  CheckBox checkBox;
              }
          }
          

          【讨论】:

            【解决方案5】:

            替换此代码

            @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        int index = (Integer)buttonView.getTag();
            
                        if(index==0)
                        {
                            if (isChecked)
                            {
                                for (int i = 0; i < mapList.size(); i++)
                                {
                                    MultipleSelect obj = (MultipleSelect) getMapList().get(i);
            //                                  obj.setSelected(false);
            //                                  arrayList2.set(i, obj);
                                    if(obj.isSelected())
                                    {
                                        selectedList.set(i, false);
                                    }
                                    else
                                    {
                                        selectedList.set(i, true);
                                    }
            
                                }
                            }
                            else
                            {
                                selectedList.set(index, false);
                            }
                        }
                        else
                        {
                            if (isChecked)
                            {
                                selectedList.set(index, true);
                            }
                            else
                            {
                                selectedList.set(index, false);
            
                                selectedList.set(0, false);
                            }
                        }
                        notifyDataSetChanged();
                    }
                });
                return convertView;
            }
            
            public static class ViewHolder {
                TextView textView;
                CheckBox checkBox;
            }
            

            【讨论】:

              猜你喜欢
              • 2014-07-14
              • 2020-12-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-11-06
              • 2015-12-20
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多