简体   繁体   English

如何更改firebase中的数据并检查id自动增量

[英]How to change data in firebase and check id auto increment

Hye, my problem is to rewrite if the user put the same nameItem that will change the price and the quantityItem, if not the item will add new id. Hye,我的问题是如果用户输入相同的 nameItem 将更改价格和数量项目,则重写,否则该项目将添加新的 id。 i also do auto increment on id.我也在 id 上做自动递增。

My Database :我的数据库: 在此处输入图片说明

This is how i try but when the name == nameItem can't read and run on else in on DataChange.这就是我尝试的方式,但是当 name == nameItem 无法在 DataChange 上读取和运行时。

if(i != 0){
        for(id1 = 1; id1<=i; id1++){
            if(id1 != 0){

                final DatabaseReference reff1 = FirebaseDatabase.getInstance().getReference().child("CartList").child(userID).child(String.valueOf(id1));

                reff1.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                        String carinama = dataSnapshot.child("nameItem").getValue().toString();
                        String changequantity = dataSnapshot.child("quantityItem").getValue().toString();

                        if(nameItem == carinama) {

                            int calcQuantity = Integer.parseInt(cdtb.getQuantityItem());
                            newpositionItem = newpositionItem + calcQuantity;
                            String x = String.valueOf(newpositionItem);
                            cdtb.setQuantityItem(x);
                            int price1Item = Integer.parseInt(priceItem);
                            newpositionItem = newpositionItem * price1Item;
                            cdtb.setPriceItem(d2f.format(newpositionItem));
                            reff1.setValue(cdtb);
                            id1 = i;
                        }else {
                            int calcQuantity = Integer.parseInt(changequantity);
                            newpositionItem = newpositionItem + calcQuantity;
                            String x = String.valueOf(newpositionItem);
                            cdtb.setQuantityItem(x);
                            double price1Item = Double.parseDouble(priceItem);
                            newpositionItem = newpositionItem * price1Item;
                            cdtb.setPriceItem(d2f.format(newpositionItem));
                            reff1.setValue(cdtb);
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                        Toast.makeText(getContext(), ""+databaseError, Toast.LENGTH_SHORT).show();
                    }
                });

            }else {

            }
        }
    }else{
        cdtb.setImageItem(imageUrl);
        cdtb.setNameItem(nameItem);
        cdtb.setPriceItem(d2f.format(calcPrice));
        cdtb.setSpecification(specItem);
        cdtb.setQuantityItem(positionItem);
        reff.child(String.valueOf(++maxid)).setValue(cdtb);
    }

Thank you...谢谢...

nameItem and carinama is String nameItem 和 carinama 是String

in java to compare two string you must use equals method not "=="在java中比较两个字符串你必须使用equals方法而不是“==”

because == will compare object location in memory and not the content string因为 == 将比较内存中的对象位置而不是内容字符串

so you code condition will be所以你的代码条件将是

if(nameItem.equals(carinama)){
           //your code here 
 }

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

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