У меня есть общая цена в методе OnBindViewHolder адаптера, как отправить эти данные обратно в CartActivity, чтобы назначить заданный текст для TextView в CartActivity. Если я go для Bundle, чтобы отправить данные к деятельности с адаптера или есть какой-либо другой способ сделать это. спасибо Мой код адаптера, который расширяет FirestoreAdapter
public class ItemCartRecyelerAdapter extends
FirestoreAdapter<ItemCartRecyelerAdapter.ViewHolder>{
private static final String TAG = "ItemRecyelerAdapter";
private int TotalPrice = 0;
private Context context;
public interface OnItemSelectedListener {
void OnItemSelected(DocumentSnapshot item);
}
private OnItemSelectedListener mListener;
public ItemCartRecyelerAdapter(Query query, OnItemSelectedListener listener) {
super(query);
mListener = listener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new ViewHolder(inflater.inflate(R.layout.item_cart_adapter,parent,false));
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.bind(getSnapshot(position), mListener);
Attachment attachment = getSnapshot(position).toObject(Attachment.class);
TotalPrice += attachment.getItem_price() * attachment.getItem_quantity();
Log.d(TAG, "onBindViewHolder: FinalCrossToatal: " + TotalPrice)
}
public void deleteItem(int position){
getSnapshot(position).getReference().delete();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView item_name, item_company, item_price, discount_price, discount;
ImageView item_image, subtract_image;
ElegantNumberButton quantityPicker;
int TotalPrice = 0 ;
public ViewHolder(@NonNull View itemView) {
super(itemView);
item_image = itemView.findViewById(R.id.itemImageView);
item_company = itemView.findViewById(R.id.item_company);
item_price = itemView.findViewById(R.id.item_price);
item_name = itemView.findViewById(R.id.item_name);
quantityPicker = itemView.findViewById(R.id.quantityPicker);
subtract_image = itemView.findViewById(R.id.subtract_image);
discount_price = itemView.findViewById(R.id.item_discount_price);
discount = itemView.findViewById(R.id.discount);
}
public void bind(final DocumentSnapshot snapshot, final OnItemSelectedListener listener) {
final Attachment attachment = snapshot.toObject(Attachment.class);
Resources resources = itemView.getResources();
TotalPrice += (attachment.getItem_price() * attachment.getItem_quantity());
Log.d(TAG, "bind: Totalss1 = " + TotalPrice);
item_name.setText(attachment.getItem_name());
item_company.setText(attachment.getItem_brand());
discount.setText(String.valueOf(attachment.getItem_discount()) + "%\noff");
quantityPicker.setNumber(String.valueOf(attachment.getItem_quantity()));
if (attachment.getItem_discount() != null && attachment.getItem_discount() != 0) {
subtract_image.setVisibility(View.VISIBLE);
discount_price.setVisibility(View.VISIBLE);
discount.setVisibility(View.VISIBLE);
Integer discountedPrice = attachment.getItem_price() * attachment.getItem_discount() / 100;
Integer priceDiscounted = attachment.getItem_price() - discountedPrice;
discount_price.setText(String.valueOf(priceDiscounted));
}
Log.d(TAG, "bind: Urs: " + attachment.getUrls());
for (Map.Entry<String, String> result : attachment.getUrls().entrySet()) {
String key = result.getKey();
String value = result.getValue();
Log.d(TAG, "bind: Urls+valuew" + key + value);
//Load Image
Glide.with(item_image.getContext())
.load(value)
.into(item_image);
}
quantityPicker.setRange(0, 10);
quantityPicker.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
@Override
public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
Log.d(TAG, "onValueChange: postion " + attachment.getItem_id());
Log.d(TAG, String.format("oldValue: %d newValue: %d", oldValue, newValue));
passData(newValue, attachment.getItem_id(), oldValue);
}
private void passData(int newValue, String item_id, int oldValue) {
// Go to the details page for the selected restaurant
//sharing to seperate cart node in store
if (newValue == 0) {
FirebaseFirestore updateQ = FirebaseFirestore.getInstance();
DocumentReference CartREf = updateQ.collection("Cart")
.document(item_id);
CartREf.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: Succed to quantity");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: Failed to change cart");
}
});
FirebaseFirestore dQ = FirebaseFirestore.getInstance();
DocumentReference QuanityRef = dQ.collection("fruits & vegetables")
.document("UyGXpk2n1A6mHsUcYjCi")
.collection("Organic Fruits")
.document(item_id);
QuanityRef.update("item_quantity", newValue).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: Success updated item_quanity in Products");
}
});
}
Log.d(TAG, "passData: new update StrARTED");
FirebaseFirestore updateQ = FirebaseFirestore.getInstance();
DocumentReference CartREf = updateQ.collection("Cart")
.document(item_id);
CartREf.update("item_quantity", newValue).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: Succed to quantity");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: Failed to change cart");
}
});
Log.d(TAG, "passData: new update StrARTED");
FirebaseFirestore dQ = FirebaseFirestore.getInstance();
DocumentReference QuanityRef = dQ.collection("fruits & vegetables")
.document("UyGXpk2n1A6mHsUcYjCi")
.collection("Organic Fruits")
.document(item_id);
QuanityRef.update("item_quantity", newValue).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: Succed to quantity");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: Failed to change cart");
}
});
}
});
int items_price = attachment.getItem_price() * attachment.getItem_quantity();
item_price.setText("INR " + items_price + "Rs");
//Click Listener
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
listener.OnItemSelected(snapshot);
}
}
});
}
}
}
это метод:
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.bind(getSnapshot(position), mListener);
Attachment attachment = getSnapshot(position).toObject(Attachment.class);
TotalPrice += attachment.getItem_price() * attachment.getItem_quantity();
Log.d(TAG, "onBindViewHolder: FinalCrossToatal: " + TotalPrice)