添加从我的RecycleView中选择的项目

时间:2018-05-27 06:17:36

标签: java android firebase firebase-realtime-database

我只想将从RecycleView中选择的项目添加到Firebase中,但它会将RecycleView中的所有项目添加到Firebase中。

    @Override
public void onAddClick(int position) {
    FoodInfo selectedItem = mfoodinfo.get(position);
    final String selectedKey = selectedItem.getFoodname();

    StorageReference imageRef = firebaseStorage.getReferenceFromUrl(selectedItem.getImageUrl());
    imageRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {

            final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Order");
            databaseReference.child(selectedKey).setValue(mfoodinfo);
            Toast.makeText(Menu.this, "Order Successful!!", Toast.LENGTH_SHORT).show();
        }
    });

}

有人帮我解决了吗?

1 个答案:

答案 0 :(得分:2)

试试这段代码: (将&#39; mfoodinfo&#39;替换为&#39; selectedItem&#39;)

    @Override
public void onAddClick(int position) {
    final FoodInfo selectedItem = mfoodinfo.get(position);
    final String selectedKey = selectedItem.getFoodname();

    StorageReference imageRef = firebaseStorage.getReferenceFromUrl(selectedItem.getImageUrl());
    imageRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {

            final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Order");
            databaseReference.child(selectedKey).setValue(selectedItem);
            Toast.makeText(Menu.this, "Order Successful!!", Toast.LENGTH_SHORT).show();
        }
    });

}