android:包含回收者视图的片段中的复选框用法

时间:2018-10-02 10:18:36

标签: android checkbox android-recyclerview recycler-adapter

在我有关健康的应用程序中,有一个片段,医生可以在其中添加或拒绝患者到他/她自己的患者列表中。我使用recyclerView来显示所有患者,在每个项目中都有有关患者的信息,例如姓名,生日,城市,以及checkBox,它显示是否添加了患者。页面顶部还有另一个checkBox用于选择所有患者,还有添加和拒绝textViews。我想使用这些textViews来添加或拒绝从他们自己的checkBoxes中选择的患者。单击添加textView之后,患者将添加到Firebase数据库。我无法执行该功能。该怎么办?这是我的代码:

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

这是我的适配器类代码:

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

1 个答案:

答案 0 :(得分:0)

我建议您用自定义模型的一个arrayList替换字符串arrayLists,并且您也可以在复选框中使用一个布尔值表示数据。例如,创建一个这样的模型:

class YourModel()
{
  String hastaAdi;
  String hastaOzellikleri;
  //... another variables
  Boolean isAdded;

  //... set & get or other methods
}

现在,您应该将ArrayList传递给适配器,并通过此arrayList设置recyclerView小部件的值。然后,您可以使用iwego布尔值初始化或更改checkBox的值。