从Firebase获得双倍 [英] Get Double From Firebase

查看:46
本文介绍了从Firebase获得双倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用dataSnapshot从firebase对象获取双精度,但是每次尝试时,我都会收到一条错误消息,指出 java.lang.ClassCastException:java.lang.Long无法转换为Java.lang.Double ,当我尝试将其强制转换为Long只是为了观察将要发生的情况时,出现此错误 java.lang.ClassCastException:无法将java.lang.Double强制转换为java.lang.长,因此总而言之,反之亦然.这是我用来在Firebase dataSnapshot中获得总数的代码:

  mDatabaseReference.child(mAuth.getCurrentUser().getUid()).addListenerForSingleValueEvent(new ValueEventListener(){@Override公共无效onDataChange(DataSnapshot dataSnapshot){如果(!isPrinted){System.out.println("Children" + dataSnapshot.getChildrenCount());对于(DataSnapshot快照:dataSnapshot.getChildren()){双倍价格=(双倍)snapshot.child("total").getValue();System.out.println("Price:" + String.valueOf(price));}isPrinted = true;}}@Override公共无效onCancelled(DatabaseError databaseError){}}); 

这是我用来将总数保存在Firebase上的代码:

  double total = holder.quantity *价格;dataSnapshot.child(code).child("total").getRef().setValue(total); 

我希望使用 Double 从firebase检索总数,但是它为我提供了 ClassCastException .请协助解决.我还尝试在

解决方案

我认为我可以从您的数据库屏幕快照中看到问题.您有一个 total 节点的值为"5.16".同时,另一个 total 的值是"0"(我还假设您有更多的 total 节点,其值为"0").

因此,无论您做什么,都会得到 ClassCastException ,因为"5.16"被视为 Double ,而"0"被视为-您正在同一查询中检索两者.

最简单的解决方案是将"0"更改为"0.0".这将使所有 total 节点都具有 Double 值.然后,您只需要调用以下内容即可检索值:-

 双倍价格=(双倍)snapshot.child("total").getValue(); 

I am trying to get a double from firebase object using dataSnapshot but each time I try to, I'm getting an error saying that java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double and when I try to maybe Cast it to a Long just to observe what's gonna happen I get this Error java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Long, so in short the error is vice versa. This is the code I'm using to get my total in Firebase dataSnapshot:

 mDatabaseReference.child(mAuth.getCurrentUser().getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            if (!isPrinted) {
                System.out.println("Children" + dataSnapshot.getChildrenCount());
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Double price = (Double) snapshot.child("total").getValue();
                    System.out.println("Price: " + String.valueOf(price));
                }
                isPrinted = true;
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

and this is the code I'm using to save the total on firebase:

double total = holder.quantity * price;
                   dataSnapshot.child(code).child("total").getRef().setValue(total);

I'm looking to retrieve the total from firebase using Double but it gives me the ClassCastException. Please help with a solution. I also tried looking here for a solution but did not find anything

This is my FirebaseDatabase where I'm querying from:

解决方案

I think I can see the problem from your Database Screenshot. You have one total node's value as "5.16". While, another total's value is "0" (I also assume that you have a lot more total nodes with values as "0").

So, you're getting a ClassCastException no matter what you do because "5.16" is treated as a Double, while "0" is treated as a Long - and you're retrieving both in the same query.

The simplest solution is to change your "0" to "0.0". This would make all total nodes hold Double values. Then, you just have to call the following to retrieve the value :-

Double price = (Double) snapshot.child("total").getValue();

这篇关于从Firebase获得双倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆