Defining Constructors in Model Class (Get Data with Cloud Firestore)

心不动则不痛 提交于 2020-08-10 22:53:53

问题


I added some data in Cloud Firestore enter image description here

in which I am having String title, String desc, String[] photos

I want to get that data in MyClass.class so I provided Parameterized Constructor and Getter & Setters in the class.

But the error said Converting to Arrays is not supported, please use Lists instead

This is my code for logical part:

FirebaseFirestore db = FirebaseFirestore.getInstance();

        db.collection("gallery")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d("MyTag", document.getId() + " => " + document.getData());
                                MyClass obj = document.toObject(MyClass.class);
                            }
                        } else {
                            Log.d("Main", "Error getting documents: ", task.getException());
                        }
                    }
                });

This is my Class:

public class MyClass {

    String title;
    String desc;
    String[] photos;

    public MyClass(){}

    public MyClass(String title, String desc, String[] photos) {
        this.title = title;
        this.desc = desc;
        this.photos = photos;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String[] getPhotos() {
        return photos;
    }

    public void setPhotos(String[] photos) {
        this.photos = photos;
    }

And I am getting this error at MyClass obj = document.toObject(MyClass.class) :

FATAL EXCEPTION: main
                  Process: com.developtrainmaintain.firebaserecycle, PID: 3836
                  java.lang.RuntimeException: Could not deserialize object. Converting to Arrays is not supported, please use Lists instead (found in field 'photos')
                      at com.google.android.gms.internal.firebase-firestore.zzko.zzb(Unknown Source)
                      at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
                      at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
                      at com.google.android.gms.internal.firebase-firestore.zzko.zzb(Unknown Source)
                      at com.google.android.gms.internal.firebase-firestore.zzko$zza.zza(Unknown Source)
                      at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
                      at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
                      at com.google.firebase.firestore.DocumentSnapshot.zza(Unknown Source)
                      at com.google.firebase.firestore.QueryDocumentSnapshot.zza(Unknown Source)
                      at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source)
                      at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(Unknown Source)
                      at com.developtrainmaintain.firebaserecycle.MainActivity$1.onComplete(MainActivity.java:48)
                      at com.google.android.gms.tasks.zzj.run(Unknown Source)
                      at android.os.Handler.handleCallback(Handler.java:733)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:5017)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                      at dalvik.system.NativeStart.main(Native Method)

Please help me with this problem :(


回答1:


The error message you're seeing is this:

Could not deserialize object. Class com.developtrainmaintain.firebaserecycle.MyClass does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped

All JavaBean type objects should have a no-arg constructor so it's possible to create a new instance of them without having to guess how to pass parameters in the constructor. So add a no-arg constructor to your class:

public MyClass() {}



回答2:


Since you are providing a parameterised constructor in your class, default no argument constructor in no longer available. Define a no arg constructor, then it should work

public MyClass() {
    // set variables initial default value if any or leave empty
}



回答3:


public class MyClass {

    String title;
    String desc;
    ArrayList<String> photos;

    public MyClass(){}

    public MyClass(String title, String desc, ArrayList<String> photos) {
        this.title = title;
        this.desc = desc;
        this.photos = photos;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public ArrayList<String> getPhotos() {
        return photos;
    }

    public void setPhotos(ArrayList<String> photos) {
        this.photos = photos;
    }
}

Firebase or FireStore doesnot support arrays using List or Map classes instead of Arrays



来源:https://stackoverflow.com/questions/49843231/defining-constructors-in-model-class-get-data-with-cloud-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!