Firebase DatabaseException:无法将java.lang.String类型的对象转换为package.Subscription

时间:2017-12-13 03:52:46

标签: java android firebase-realtime-database

Error log:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.netgalaxystudios.timeclock.Subscription

 at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                                at com.mypackage.appname.RegisterSubscriptionActivity$1.onDataChange(RegisterSubscriptionActivity.java:81)

Points to :

Subscription subscription = child.getValue(Subscription.class);

Firebase DB:

enter image description here

我遵循了教程,这似乎正是他们如何构建它,所以不确定为什么它不喜欢我转换到我的班级?

RegisterSubscriptionActivity.java:

public class RegisterSubscriptionActivity extends Activity {

        //Firebase Database References
        DatabaseReference mDatabase;
        DatabaseReference mDatabaseMicro;
        DatabaseReference mDatabaseSmall;
        DatabaseReference mDatabaseMedium;
        DatabaseReference mDatabaseLarge;
        DatabaseReference mDatabaseEnterprise;
        DatabaseReference mListItemRef;

        ArrayList subscriptionInfo;

        ArrayList<Subscription> myListItems;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register_subscription);

            myListItems = new ArrayList<Subscription>();

            mDatabase = FirebaseDatabase.getInstance().getReference("Subscription");
            mDatabase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Iterable<DataSnapshot> children = dataSnapshot.getChildren();


                    for(DataSnapshot child : children) {
                        Subscription subscription = child.getValue(Subscription.class);
                        myListItems.add(subscription);
                    }


                }


                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.d("Cancelled",databaseError.toString());
                }
            });

            Log.i("LIST OF DATA: ", myListItems.toString());        



        }

    }

Subscription.java:

public class Subscription {

        String name, number, price;

        public Subscription() {}

        public Subscription(String name, String number, String price) {
            this.name = name;
            this.number = number;
            this.price = price;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }

        public String getPrice() {
            return price;
        }

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


        public Map<String, Object> toMap() {
            HashMap<String, Object> result = new HashMap<>();
            result.put("NAME", name);
            result.put("NUMBER", number);
            result.put("PRICE", price);
            return result;
        }
    }

1 个答案:

答案 0 :(得分:0)

要获取name, number and price的值,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("Subscription").child("Micro");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String name = dataSnapshot.child("name").getValue(String.class);
        String number = dataSnapshot.child("number").getValue(String.class);
        String price = dataSnapshot.child("price").getValue(String.class);
        Log.d("TAG", name + " / " + price + " / " + price);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);