Android 如何将数据从方法BindView中的适配器发送到活动?

Android 如何将数据从方法BindView中的适配器发送到活动?,android,android-layout,google-cloud-firestore,Android,Android Layout,Google Cloud Firestore,我在adapter OnBindViewHolder方法中有一个总价,如何将数据发送回CartActivity,以在CartActivity中的TextView上分配设置文本。我应该使用Bundle从适配器向activity发送数据,还是有其他方法。谢谢 扩展FirestoreAdapter的我的适配器代码 public class ItemCartRecyelerAdapter extends FirestoreAdapter<ItemCartRecyelerAdapter.V

我在adapter OnBindViewHolder方法中有一个总价,如何将数据发送回CartActivity,以在CartActivity中的TextView上分配设置文本。我应该使用Bundle从适配器向activity发送数据,还是有其他方法。谢谢 扩展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);
                    }
                }

            });

        }

    }
}

尝试在onBindViewHolder中发送数据
SomeActivity=newsomeActivity();
Bundle args=新Bundle();
参数putInt(“总价”,总价);

fragment.setArguments(args)

使用接口从RecycleServiceAdapter与活动进行通信。

创建接口,活动实现此接口

public interface OnItemClick {
    void onClick (String value);
}
创建适配器时(最后一个参数是此接口)

//输入适配器

private OnItemClick mCallback;

RecycleAdapter(Context context,List<HashMap<String, String>>     onlineData,OnItemClick listener){
    this.onlineData = onlineData;
    this.context = context;
    this.mCallback = listener;
 }
....

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)
    mCallback.onClick(TotalPrice);
private-onite-mclick-mCallback;
RecycleAdapter(上下文上下文、联机列表数据、OnItemClick侦听器){
this.onlineData=onlineData;
this.context=上下文;
this.mCallback=侦听器;
}
....
public void onBindViewHolder(@NonNull final ViewHolder,final int position){
holder.bind(获取快照(位置),mListener);
附件Attachment=getSnapshot(position).toObject(Attachment.class);
总价+=附件.getItem_价格()*附件.getItem_数量();
Log.d(标签“onBindViewHolder:FinalCrossTotal:+TotalPrice”)
onClick(总价);

您是否尝试过使用意图?没有,我尝试过使用意图,但没有成功。您是否建议使用意图是最好的方法?在活动之间发送数据时,这是一种常见的方法。谢谢!它解决了我的问题95%。唯一的一点是,当我更改数量值时,我的总价是错误的。只有当我重新启动活动时,我才会使用意图它再次显示了正确的值。我所做的正如你所说的@Override public void onClick(int totalCost){Log.d(TAG,“onClick:Total calsrh:”+totalCost);Total_text.setText(String.valueOf(totalCost));Total2.setText(String.valueOf(totalCost));}
public class MainActivity extends AppCompatActivity implements OnItemClick {
 recycleAdapter = new RecycleAdapter(MainActivity.this,onlineData, this);
            recyclerView.setAdapter(recycleAdapter);

 @Override
 void onClick (String value){
//Use the data in Activity
}
private OnItemClick mCallback;

RecycleAdapter(Context context,List<HashMap<String, String>>     onlineData,OnItemClick listener){
    this.onlineData = onlineData;
    this.context = context;
    this.mCallback = listener;
 }
....

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)
    mCallback.onClick(TotalPrice);