mardi 2 octobre 2018

android: checkbox usage in fragment which contains recycler view

in my app about health, there is a fragment in which doctor can add or deny patient to his/her own patient list. I use a recyclerView to show all patients, in each items there are informations about patiens like names, birthday, city and a checkBox which shows patient is added or not. Top of the page there is another checkBox to select all patients, also there are add and deny textViews. I want to use these textViews to add or deny patients which is selected of their own checkBoxes. I could not do that features.How can i do? Here is my codes:

public class FragmentHastaBasvurulari extends Fragment {

private ArrayList<String> hastaAdi=new ArrayList<>();
private ArrayList<String> hastaOzellikleri=new ArrayList<>();
private ArrayList<String> hastaliklar=new ArrayList<>();
private ArrayList<String> hastaOnayi=new ArrayList<>();
private ArrayList<Users> hastalar=new ArrayList<>();


private FirebaseAuth mAuth;
private FirebaseUser firebaseUser;
private FirebaseDatabase database;
private DatabaseReference myRef;
private RecyclerView rvHastaBasvuru;
  private CheckBox checkBoxHepsiniSec;

private TextView tvReddet,tvOnayla;


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getActivity().setTitle("Hasta Başvuruları");

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_hasta_basvurulari, container, false);

    mAuth=FirebaseAuth.getInstance();
    firebaseUser=mAuth.getCurrentUser();
    database=FirebaseDatabase.getInstance();
    myRef=database.getReference();
    rvHastaBasvuru=(RecyclerView) view.findViewById(R.id.rvHastaBasvuru);
            checkBoxHepsiniSec=(CheckBox) view.findViewById(R.id.checkBoxHepsiniSec); 

    tvOnayla=(TextView) view.findViewById(R.id.tvOnayla);
    tvReddet=(TextView) view.findViewById(R.id.tvReddet);



    myRef.child("kullanicilar")
            .child(firebaseUser.getUid()).child("hastalar").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for(DataSnapshot ds: dataSnapshot.getChildren()){
                EklenecekHasta eklenecekHasta = ds.getValue(EklenecekHasta.class);

                    DatabaseReference hastaRef= FirebaseDatabase.getInstance().getReference().child("kullanicilar").child(eklenecekHasta.getUserId());
                    hastaRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (dataSnapshot.exists()) {
                                Users hasta = dataSnapshot.getValue(Users.class);

                                String sehir, dogumTarihi,hastaliklar1;

                                if(hasta.getSehir().equals("Seçiniz")){
                                    sehir="---";
                                } else {
                                    sehir=hasta.getSehir();
                                }
                                if(hasta.getDogumtarihi().equals("../../....")){
                                    dogumTarihi="---";
                                } else {
                                    dogumTarihi=hasta.getDogumtarihi();
                                }
                                if(hasta.getHastaliklar().equals("")){
                                    hastaliklar1="---";
                                } else {
                                    hastaliklar1=hasta.getHastaliklar();
                                }
                                hastaAdi.add(hasta.getAd()+" "+hasta.getSoyad());
                                hastaOzellikleri.add(hasta.getCinsiyet()+"  "+sehir+"  "+dogumTarihi);
                                hastaliklar.add(hastaliklar1);
                                hastaOnayi.add(hasta.getOnay());
                                hastalar.add(hasta);

                            }

                            rvHastaBasvuru.setAdapter(adapter);
                            rvHastaBasvuru.setLayoutManager(new LinearLayoutManager(getContext()));
                            adapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onCancelled(DatabaseError error) {
                        }
                    });

            }


        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }


        HastaListesiAdapter adapter= new HastaListesiAdapter(getContext(),hastaAdi,hastaOzellikleri,hastaliklar,hastaOnayi,hastalar);

    });



    tvOnayla.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });

    tvReddet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });



    return view;
}

Here is my adapter class codes:

public class HastaListesiAdapter extends RecyclerView.Adapter<HastaListesiAdapter.ViewHolder>{

private static final String TAG="HekimListesiAdapter";

private ArrayList<String> hastaAdi=new ArrayList<>();
private ArrayList<String> hastaOzellikleri=new ArrayList<>();
private ArrayList<String> hastaliklar=new ArrayList<>();
private ArrayList<String> hastaOnayi=new ArrayList<>();
private ArrayList<Users> hastalar=new ArrayList<>();
private Context myContext;


public HastaListesiAdapter(Context myContext, ArrayList<String> hastaAdi, ArrayList<String> hastaOzellikleri,
                           ArrayList<String> hastaliklar,ArrayList<String> hastaOnayi,ArrayList<Users> hastalar) {
    this.hastaAdi = hastaAdi;
    this.hastaOzellikleri = hastaOzellikleri;
    this.myContext = myContext;
    this.hastaliklar=hastaliklar;
    this.hastaOnayi=hastaOnayi;
    this.hastalar=hastalar;
}



@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.hasta_item,parent,false);
    ViewHolder holder=new ViewHolder(view);

    return holder;
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int i) {


    holder.itemHastaAd.setText(hastaAdi.get(i));
    holder.itemHastaOz.setText(hastaOzellikleri.get(i));
    holder.itemHastaliklar.setText(hastaliklar.get(i));

    //Hasta hekim tarafından onaylanmış mı onaylanmamış mı
    if(hastaOnayi.get(i).equals("evet")){
        holder.checkBoxHastaSec.setChecked(true);
    } else if(hastaOnayi.get(i).equals("hayır")){
        holder.checkBoxHastaSec.setChecked(false);
    }

    // CheckBox ın tıklanması üzerine yapılacak işlem

    holder.checkBoxHastaSec.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

            String secilimi;
            if(buttonView.isChecked()){
                secilimi="evet";
            } else {
                secilimi="hayır";
            }

        }



    });




}

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


public class ViewHolder extends RecyclerView.ViewHolder {

    TextView itemHastaAd,itemHastaOz,itemHastaliklar;
    CheckBox checkBoxHastaSec;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        itemHastaAd=itemView.findViewById(R.id.itemHastaAd);
        itemHastaOz= itemView.findViewById(R.id.itemHastaOz);
        itemHastaliklar=itemView.findViewById(R.id.itemHastaliklar);
        checkBoxHastaSec=itemView.findViewById(R.id.checkBoxHastaSec);
    }
}




Aucun commentaire:

Enregistrer un commentaire