すべての行を選択したい。Cardviewのすべてを選択
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;
}}
CardView2
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あなたがモデルクラスにブール値を入れれば私はコードの多くを変更しなければならないので、このコードはほぼ真実ですすべてを選択するだけです –