如何使用recyclerview和onclick更新firestore上的字段?

ejk8hzay  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(325)

我有一个 RecyclerView 使用 Firestore Adapters . 我的手机上有个按钮 itemView . 我试图使它这样,当按钮被按下时,它将改变布尔字段 done 从虚假到真实。我正在努力理解如何把这两者联系起来。
目前,我能读懂的时候里面的哪个位置 RecyclerView 并在我的电脑上显示 Log . 但差不多就是这样。这是我的密码:
接口:

public interface OnListAdminClick {
    void onItemClick(int position);
}

我的适配器:

public class DonorAdapter extends FirestoreRecyclerAdapter<DonorModel, DonorAdapter.DonorViewHolder> {

private OnListAdminClick onListAdminClick;

public DonorAdapter(@NonNull FirestoreRecyclerOptions<DonorModel> options, OnListAdminClick onListAdminClick) {
    super(options);
    this.onListAdminClick = onListAdminClick;
}

@Override
protected void onBindViewHolder(@NonNull DonorAdapter.DonorViewHolder holder, int position, @NonNull DonorModel model) {
    holder.rank.setText(String.valueOf(position + 1));
    holder.donorName.setText(model.getDonorName());
    holder.donorEmail.setText(model.getDonorEmail());
    holder.hospitalName.setText(model.getHospitalName());
    holder.bookingTime.setText(model.getTime());
    holder.status.setText(model.isDone() + "");
}

@NonNull
@Override
public DonorAdapter.DonorViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_adminview_single, parent, false);
    return new DonorViewHolder(view);
}

public class DonorViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView rank;
    TextView donorName;
    TextView donorEmail;
    TextView hospitalName;
    TextView bookingTime;
    TextView status;
    Button done_btn;

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

        rank = itemView.findViewById(R.id.adminview_position);
        donorName = itemView.findViewById(R.id.list_username);
        donorEmail = itemView.findViewById(R.id.list_email);
        hospitalName = itemView.findViewById(R.id.list_hospital_name);
        bookingTime = itemView.findViewById(R.id.list_booking_time);
        status = itemView.findViewById(R.id.status_booking);
        done_btn = itemView.findViewById(R.id.done_btn);

        done_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
         onListAdminClick.onItemClick(getAdapterPosition());
    }
}
}

在我的main上定义onnitemclick:

adapter = new DonorAdapter(options, new OnListAdminClick() {
        @Override
        public void onItemClick(int position) {
            Log.d("ITEM_CLICK", "Clicked the item: " + position);

        }
    });

最后,我的firestore:

vc6uscn9

vc6uscn91#

我在适配器类中看到,您将position传递给onitemclick()方法,这不是很有帮助,因为它只表示recyclerview中单击项的位置。但是,传递“donoruid”将帮助您解决问题。这可以很容易地完成,绕过“donoruid”值到donorviewholder的构造函数。现在,接口的声明应该如下所示:

public interface OnListAdminClick {
    void onItemClick(int donorUID);
}

在活动中,可以使用以下代码行将“done”属性更新为true:

adapter = new DonorAdapter(options, new OnListAdminClick() {
    @Override
    public void onItemClick(int donorUID) {
        FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
        CollectionReference bookingRef = rootRef.collection("UserBooking").document(donorUID).collection("Booking");
        Query donorUidQuery = bookingRef.whereEqualTo("donorUID", donorUID);
        donorUidQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        document.getReference().update("done", true);
                    }
                }
            }
        });
    }
});

相关问题