1

Ich möchte alle Zeilen auswählen.Alles auswählen in Kartenansicht

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

Ich möchte alle Zeilen überprüft werden, wenn SelectAll Checkbox aktiviert ist.

+0

Ich würde einen Boolean in Modellklasse setzen und es entsprechend aktualisieren und notifydatasetchanged Methode aufrufen. In onbindviewholder basierend auf diesem Boolean setze ich das Kontrollkästchen –

+0

@prashanthDebbadwar können Sie mir bitte im Detail Prashanth Bro sagen, denn wenn ich boolean in Modellklasse setzen dann habe ich Änderungen in vielen der Code und dieser Code ist fast wahr, nur wenn wir alles auswählen es wählt nur ein –

Antwort

0
CheckBox cb; 
for (int i = 0; i < rv.getChildCount(); i++) { 
    cb = (CheckBox)rv.getChildAt(i).findViewById(R.id.c2); 
    cb.setChecked(selectall.isChecked()); 
} 

so ist es das, was ich hav die Antwort zu tun jetzt.

0

Versuchen

cb.setChecked(isChecked) 

statt:

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

das ist, was ich getan hatte Ich möchte, dass, wenn ich alle auswählen, es alle auswählen sollte ...... @ user2605841 –

+0

für stList können wir nicht einen loopr schreiben und alles einstellen? –

Verwandte Themen