当addValueEventListener给出datasnapshot的值时,addChildEventListener显示错误。为什么?

时间:2017-07-20 19:21:00

标签: java android firebase firebase-realtime-database

数据库结构

Databse Strucutre

这个列表器为模型类提供了价值

 ref.child(stateName).child(cityName).child("business").child(user).child("counter").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            ShopCategoryModel  Smodel =  dataSnapshot.getValue( ShopCategoryModel.class);




            servicetype = Smodel.getServicetype();
            int orderleft = Smodel.getOrderleft();
            int uploadsleft = Smodel.getUploadsleft();
            int referralpionts = Smodel.getReferralpionts();
            renewaldate = Smodel.getRenewaldate();
            int vistors = Smodel.getVisitors();
            int ratings = Smodel.getRatings();

            ac_TotalVisitors.setText(""+vistors);
            ac_ratingBar.setRating(Float.parseFloat(String.valueOf(ratings)));
            ac_Shope_ratings.setText(""+ratings);

            ac_orderleft.setText(""+orderleft);
            ac_Uploadsleft.setText(""+uploadsleft);
            ac_Referralpoints.setText(""+referralpionts);
            ac_renewdate.setText(renewaldate);








            }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

应用addChildEventListener时显示错误

 ref.child(stateName).child(cityName).child("business").child(user).child("counter").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {


            ShopCategoryModel  Smodel =  dataSnapshot.getValue( ShopCategoryModel.class);



            servicetype = Smodel.getServicetype();
            int orderleft = Smodel.getOrderleft();
            int uploadsleft = Smodel.getUploadsleft();
            int referralpionts = Smodel.getReferralpionts();
            renewaldate = Smodel.getRenewaldate();

            int vistors = Smodel.getVisitors();
            int ratings = Smodel.getRatings();

            ac_TotalVisitors.setText(""+vistors);
            ac_ratingBar.setRating(Float.parseFloat(String.valueOf(ratings)));
            ac_Shope_ratings.setText(""+ratings);

            ac_orderleft.setText(""+orderleft);
            ac_Uploadsleft.setText(""+uploadsleft);
            ac_Referralpoints.setText(""+referralpionts);
            ac_renewdate.setText(renewaldate);


        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

此后出现此错误

 Process: mahendergarh.bhupendra.shaharseller, PID: 18837
                                                                                 com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type mahendergarh.bhupendra.shaharseller.model.ShopCategoryModel
                                                                                     at com.google.android.gms.internal.adw.zzb(Unknown Source)
                                                                                     at com.google.android.gms.internal.adw.zza(Unknown Source)
                                                                                     at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                                     at mahendergarh.bhupendra.shaharseller.Sign_in.Account$3.onChildAdded(Account.java:255)
                                                                                     at com.google.android.gms.internal.uv.zza(Unknown Source)
                                                                                     at com.google.android.gms.internal.aaf.zzIo(Unknown Source)
                                                                                     at com.google.android.gms.internal.aal.run(Unknown Source)
                                                                                     at android.os.Handler.handleCallback(Handler.java:746)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

m在不同时间使用每个听众。 请告诉我发生这种情况的原因为什么在addChildEventListener中出现模型类错误。提前超过!!!!

1 个答案:

答案 0 :(得分:0)

ChildEventListener会为您附加位置的每个孩子触发,而ValueEventListener会触发该位置本身。

因此,如果将侦听器附加到数据库中的相同位置,则onChildAdded将触发该节点的各个属性。所以dateorderLeft

如果您有查询或收藏品,通常需要ChildEventListener,而您不在此处。