【问题标题】:Select all in Cardview在 Cardview 中全选
【发布时间】:2016-12-01 09:20:12
【问题描述】:

我想选择所有行。

CustomAdapter2

public class CustomAdapter2 extends RecyclerView.Adapter<CustomAdapter2.EmpDataViewHolder> {

    public ArrayList<Employee> emp_arr, filterList;
    private Context context;
    public ArrayList<Employee> getEmpList;

    public CustomAdapter2(Context context, ArrayList<Employee> emp_arr) {
        this.emp_arr= emp_arr;
        this.context = context;
        this.filterList = new ArrayList<Employee>();
        this.filterList.addAll(this.emp_arr);
    }

    @Override
    public CustomAdapter2.EmpDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.customcard1, parent, false);
        EmpDataViewHolder edvh = new EmpDataViewHolder(v);
        return edvh;
    }

    @Override
    public void onBindViewHolder(final CustomAdapter2.EmpDataViewHolder holder, final int position) {

        Employee employee = new Employee();
        employee = filterList.get(position);
        int pos = position;

        final String emp_ID = "" + employee.getEmpid();
        holder.txt_id.setText(emp_ID);

        holder.txt_fname.setText(employee.getEmpfname());
        holder.txt_lname.setText(employee.getEmplname());

        holder.cb1.setChecked(employee.isSelected());
        holder.cb1.setTag(employee);
        holder.cb1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                Employee contact = (Employee) cb.getTag();

                contact.setSelected(cb.isChecked());
                emp_arr.get(position).setSelected(cb.isChecked());

                Toast.makeText(
                        v.getContext(),
                        "Clicked on Checkbox: " + cb.getText() + " is "
                                + cb.isChecked(), Toast.LENGTH_LONG).show();
            }
        });

        holder.cv.setTag(R.string.KeyForCV, position);
        holder.cv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CardView cv = (CardView) v;
                Employee emp = filterList.get(position);
            }
        });}

    @Override
    public int getItemCount() {
        return filterList.size();
    }

    static class EmpDataViewHolder extends RecyclerView.ViewHolder{

        CardView cv;
        TextView txt_id;
        TextView txt_fname;
        TextView txt_lname;
        CheckBox cb1;
        CheckBox cb;
        Boolean checked;


        public EmpDataViewHolder(View itemView) {
            super(itemView);


            cv = (CardView) itemView.findViewById(R.id.cv1);
            txt_id = (TextView) itemView.findViewById(R.id.id);
            txt_fname = (TextView) itemView.findViewById(R.id.fname);
            txt_lname = (TextView) itemView.findViewById(R.id.lname);
            cb1 = (CheckBox) itemView.findViewById(R.id.c2);

        }
    }
    private ArrayList<Employee> getStudentist() {
        return emp_arr;
    }}

卡片视图2

public class Cardview2 extends AppCompatActivity {

    CustomAdapter2 cu;
    CheckBox selectall,c2;

    private ArrayList<Employee> Emplist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cardview2);


        RecyclerView rv = (RecyclerView) findViewById(R.id.rv1);
        rv.setHasFixedSize(true);

        final ArrayList<Employee> arr = InitializeData();

        final LinearLayoutManager llm = new LinearLayoutManager(Cardview2.this);
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);

        cu = new CustomAdapter2(Cardview2.this, arr);
        rv.setAdapter(cu);

        registerForContextMenu(rv);


        selectall= (CheckBox) findViewById(R.id.c1);
        Emplist=new ArrayList<Employee>();

        for (int i = 1; i <=Emplist.size(); i++) {
            Employee st = new Employee();

            Emplist.add(st);
        }

        selectall.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String data = "";
                ArrayList<Employee> stList = ((CustomAdapter2) cu)
                        .emp_arr;

                CheckBox cb;
                cb = (CheckBox) findViewById(R.id.c2);
    try { for (int i = 0; i < stList.size(); i++) {
        Employee employee = stList.get(i);
        if (employee.isSelected() == false) {
            cb.setChecked(selectall.isChecked());
        }
    }}catch (Exception e){
    e.printStackTrace();}[enter image description here][1]}

        });


    }
        private ArrayList<Employee> InitializeData () {
            ArrayList<Employee> arr_emp = new ArrayList<>();



            DAL dal = new DAL(Cardview2.this);
            dal.OpenDB();
            arr_emp = dal.AllSelectQryForTabEmpData1();
            dal.CloseDB();
            return arr_emp;}}

我希望在启用 Selectall 复选框时检查所有行。

【问题讨论】:

  • 我会在模型类中放置一个布尔值并相应地更新它并调用 notifydatasetchanged 方法。在基于此布尔值的 onbindviewholder 中,我将设置复选框
  • @prashanthDebbadwar 能否请您详细告诉我 prashanth 兄弟,因为如果我将布尔值放在模型类中,那么我必须更改许多代码,并且当我们选择所有代码时,该代码几乎是正确的只选择一个

标签: android list arraylist android-recyclerview android-cardview


【解决方案1】:

试试

cb.setChecked(isChecked)

而不是:

Employee employee = stList.get(i);
    if (employee.isSelected() == false) {
        cb.setChecked(selectall.isChecked());
    }

【讨论】:

  • 这就是我所做的我想要如果我选择全选它应该全选...... @user2605841
  • for stList 我们不能写一个loopr并设置所有吗?
【解决方案2】:
CheckBox cb;
for (int i = 0; i < rv.getChildCount(); i++) {
    cb = (CheckBox)rv.getChildAt(i).findViewById(R.id.c2);
    cb.setChecked(selectall.isChecked());
}

所以这就是我现在要做的事情得到了答案。

【讨论】:

    猜你喜欢
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    相关资源
    最近更新 更多