简体   繁体   English

如何使用带有 onClick 的 RecyclerView 更新 Firestore 上的字段?

[英]How to update field on Firestore using RecyclerView with onClick?

I have a RecyclerView using Firestore Adapters .我有一个使用Firestore AdaptersRecyclerView I have a button on my itemView .我的itemView上有一个按钮。 I am trying to make it so that when the button is pressed, it will change the boolean field done from false to true.我正在尝试这样做,以便在按下按钮时,它将done的 boolean 字段从 false 更改为 true。 I am struggling to understand how to tie those two together.我正在努力理解如何将这两者联系在一起。

Currently, I am able to read when which position inside of the RecyclerView with an interface and display it on my Log .目前,我可以通过界面读取RecyclerView内的哪个 position 并将其显示在我的Log上。 But that's pretty much it.但仅此而已。 Here is my code:这是我的代码:

The interface:界面:

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

My Adapter:我的适配器:

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

Defining the onItemClick on my Main:在我的 Main 上定义 onItemClick:

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

        }
    });

Lastly my Firestore:最后是我的 Firestore:

在此处输入图像描述

I see in your adapter class that you pass to the onItemClick() method the position, which is not very helpful as it represents only the position of the clicked item in the RecyclerView.我在您的适配器 class 中看到您将 position 传递给 onItemClick() 方法,这不是很有帮助,因为它仅代表 RecyclerView 中单击项目的 position。 However, passing the "donorUID" will help you solve the problem.但是,传递“donorUID”将帮助您解决问题。 This can be done very easily, bypassing the "donorUID" value to the DonorViewHolder's constructor.这可以很容易地完成,绕过 DonorViewHolder 的构造函数的“donorUID”值。 Now the declaration of your interface should look like this:现在你的接口声明应该是这样的:

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

And inside your activity, you can update the "done" property to true using the following lines of code:在您的活动中,您可以使用以下代码行将“完成”属性更新为 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);
                    }
                }
            }
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM