简体   繁体   中英

Firebase DatabaseException: Can't convert object of type java.lang.String to package.Subscription

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:

在此处输入图片说明


I followed the tutorials and this seems to be exactly how they structure it, so not sure why it doesnt like me converting to my class?

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;
        }
    }

To get the values of name, number and price , please use the following code:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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