gpt4 book ai didi

android - 在模型类中定义构造函数(使用 Cloud Firestore 获取数据)

转载 作者:行者123 更新时间:2023-12-04 23:56:44 24 4
gpt4 key购买 nike

我在 Cloud Firestore 中添加了一些数据 enter image description here

其中我有字符串标题、字符串描述、字符串[] 照片

我想在 MyClass.class 中获取该数据,所以我在类(class)中提供了Parameterized ConstructorGetter & Setters

但是报错说不支持转换为数组,请改用列表

这是我的逻辑部分代码:

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

这是我的类(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;
}

我在 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)

请帮我解决这个问题:(

最佳答案

您看到的错误消息是这样的:

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

所有 JavaBean 类型的对象都应该有一个无参数的构造函数,这样就可以创建它们的新实例,而不必猜测如何在构造函数中传递参数。因此,向您的类添加一个无参数构造函数:

public MyClass() {}

关于android - 在模型类中定义构造函数(使用 Cloud Firestore 获取数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49843231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com