简体   繁体   中英

How to update field on Firestore using RecyclerView with onClick?

I have a RecyclerView using Firestore Adapters . I have a button on my 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. 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 . 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:

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

        }
    });

Lastly my 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. However, passing the "donorUID" will help you solve the problem. This can be done very easily, bypassing the "donorUID" value to the DonorViewHolder's constructor. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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