有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓公司。谷歌。火基。数据库DatabaseException:未能转换java类型的值。将字符串改为长字符串

我在尝试将数据检索到某个对象以供使用时,在遵循firebase数据库的文档时遇到了一个错误

这是我的对象模型

public class Cart {

    private String pid, pname, price, discount, quantity;

    public Cart() {
    }

    public Cart(String pid, String pname, String price, String discount, String quantity) {
        this.pid = pid;
        this.pname = pname;
        this.price = price;
        this.discount = discount;
        this.quantity = quantity;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getDiscount() {
        return discount;
    }

    public void setDiscount(String discount) {
        this.discount = discount;
    }

    public String getQuantity() {
        return quantity;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }
}

最后,守则及;我得到的错误行被注释了

FirebaseRecyclerOptions<Cart> options =
    new FirebaseRecyclerOptions.Builder<Cart>()
    .setQuery(cartListRef.child("User View")
    .child(Prevalent.currentOnlineUser.getPhn_num())
    .child("Products"), Cart.class)
    .build();

FirebaseRecyclerAdapter<Cart, CartViewHolder> adapter =
    new FirebaseRecyclerAdapter<Cart, CartViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull CartViewHolder holder, int position, @NonNull Cart model)
        {
            holder.jProductName.setText(model.getPname());
            holder.jProductPrice.setText("Price = Rs. " +model.getPrice());
            holder.jProductQuantity.setText("Quantity = " +model.getQuantity());

        }

        @NonNull
        @Override
        public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i)
        {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_items_layout, parent,false);
            CartViewHolder holder = new CartViewHolder(view);
            return holder;
        }
    };

recyclerView.setAdapter(adapter);
adapter.startListening();

这是我在Firebase中的数据库结构

enter image description here

来自logcat的完整Stacktrace在这里

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dueaoinfo.shoppingapp, PID: 10511
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long
    at com.google.安卓.gms.internal.firebase_database.zzkt.zzb(Unknown Source:616)
    at com.google.安卓.gms.internal.firebase_database.zzkt.zza(Unknown Source:301)
    at com.google.安卓.gms.internal.firebase_database.zzkt.zzb(Unknown Source:0)
    at com.google.安卓.gms.internal.firebase_database.zzku.zza(Unknown Source:71)
    at com.google.安卓.gms.internal.firebase_database.zzkt.zzb(Unknown Source:146)
    at com.google.安卓.gms.internal.firebase_database.zzkt.zza(Unknown Source:0)
    at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
    at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
    at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
    at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)
    at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
    at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:106)
    at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)
    at 安卓.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
    at 安卓.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
    at 安卓.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
    at 安卓.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
    at 安卓.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at 安卓.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at 安卓.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at 安卓.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
    at 安卓.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at 安卓.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at 安卓.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
    at 安卓.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
    at 安卓.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1888)
    at 安卓.support.v7.widget.RecyclerView$1.run(RecyclerView.java:407)
    at 安卓.view.Choreographer$CallbackRecord.run(Choreographer.java:1092)
    at 安卓.view.Choreographer.doCallbacks(Choreographer.java:888)
    at 安卓.view.Choreographer.doFrame(Choreographer.java:816)
    at 安卓.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1078)
    at 安卓.os.Handler.handleCallback(Handler.java:873)
    at 安卓.os.Handler.dispatchMessage(Handler.java:99)
    at 安卓.os.Looper.loop(Looper.java:201)
    at 安卓.app.ActivityThread.main(ActivityThread.java:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:873)

共 (1) 个答案

  1. # 1 楼答案

    quantitydiscount是firebase数据库中的Long值,在Cart类中声明为String。这是抛出错误。声明Cart类,如下所示

    public class Cart {
    
        private String pid, pname, price;
        private Long discount, quantity;
    
        public Cart() {
        }
    
        public Cart(String pid, String pname, String price, Long discount, Long quantity) {
            this.pid = pid;
            this.pname = pname;
            this.price = price;
            this.discount = discount;
            this.quantity = quantity;
        }
    
        public String getPid() {
            return pid;
        }
    
        public void setPid(String pid) {
            this.pid = pid;
        }
    
        public String getPname() {
            return pname;
        }
    
        public void setPname(String pname) {
            this.pname = pname;
        }
    
        public String getPrice() {
            return price;
        }
    
        public void setPrice(String price) {
            this.price = price;
        }
    
        public Long getDiscount() {
            return discount;
        }
    
        public void setDiscount(Long discount) {
            this.discount = discount;
        }
    
        public Long getQuantity() {
            return quantity;
        }
    
        public void setQuantity(Long quantity) {
            this.quantity = quantity;
        }
    }
    

    希望这能解决你的问题