谁能告诉我如何在 Firebase Firestore 中存储自定义对象数组 (Android)
Can anyone tell me how can I store an array of custom objects in Firebase Firestore (Android)
谁能告诉我如何从一个应用程序将一组自定义对象存储在 Firebase Firestore (Android) 中,然后将这些对象检索回另一个应用程序(这两个应用程序都是连接到同一个 Firebase 项目)。
我尝试使用地图,我能够存储订单列表,但我无法取回 ArrayList。
这是我的做法:
Map<String, ArrayList<OrderItemModel>> map = new HashMap<>();
map.put(currentOrderNumber, orderList);
firebaseFirestore.collection("orders").document(college).set(map, SetOptions.merge())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "Order placed.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), "Couldn't place order. Please try again.", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
当我尝试使用 documentSnapshot.getData() 检索它时,它 returns Map。我试图将对象转换为 ArrayList 并在 RecyclerView 适配器中使用它,但它显示错误。
这是我的 OrderItemModel class:
public class OrderItemModel {
private String itemName;
private String itemPrice;
private String itemCount;
public OrderItemModel() {
}
public OrderItemModel(String itemName, String itemPrice, String itemCount) {
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemCount = itemCount;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemPrice() {
return itemPrice;
}
public void setItemPrice(String itemPrice) {
this.itemPrice = itemPrice;
}
public String getItemCount() {
return itemCount;
}
public void setItemCount(String itemCount) {
this.itemCount = itemCount;
}
}
我的 Firebase Firestore:
我已经尝试解决这个问题一个星期了,但仍然一无所获。我已经尝试了很多东西,但仍然找不到解决方案。如果有什么不清楚的请询问。请帮助。
谢谢。
编辑:
我正在按以下方式检索数据:
firebaseFirestore.collection("orders").document(college).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
assert documentSnapshot != null;
if (documentSnapshot.exists()) {
Map<String, Object> listMap = documentSnapshot.getData();
for (Map.Entry<String, Object> entry : listMap.entrySet()) {
Log.d(TAG, "onComplete: Key: " + entry.getKey() + ", Value: " + entry.getValue().toString());
if (entry.getKey().equals(currentOrderNumber)) {
orderList.addAll((Collection<? extends OrderItemModel>) entry.getValue());
}
}
Log.d(TAG, "onComplete: ArrayList: " + orderList.get(0).getItemName()); // This line shows error.
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(ShowOrder.this, "Order document not found!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(ShowOrder.this, "An error occurred while fetching the data.", Toast.LENGTH_SHORT).show();
}
}
});
据我了解,我已成功检索到 Map 形式的数据,但无法从中提取 ArrayList。
Log.d(TAG, "onComplete: ArrayList: " + orderList.get(0).getItemName());
上面一行显示如下错误:
2021-01-10 23:48:40.192 8747-8747/com.campuscanteen.main E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.campuscanteen.main, PID: 8747
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.campuscanteen.main.Models.OrderItemModel
at com.campuscanteen.main.ShowOrder.onComplete(ShowOrder.java:89)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:230)
at android.app.ActivityThread.main(ActivityThread.java:7815)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:526)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
编辑 2:
我按照你的建议做了一个class:
public class OrderList {
public List<OrderItemModel> modelList;
public OrderList() {
}
@Override
public String toString() {
return "OrderList{" +
"modelList=" + modelList +
'}';
}
}
并尝试了这些行来检索数据:
List<OrderItemModel> myLists = documentSnapshot.toObject(OrderList.class).modelList;
Log.d(TAG, "onComplete: ArrayList: " + myLists.toString()); // myLists here has null value.
这样列表就有了空值。请原谅我缺乏知识,请帮助。
您收到以下错误:
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.campuscanteen.main.Models.OrderItemModel
因为你试图将 HashMap 类型的对象转换为 OrderItemModel 类型的对象,这在 Java.
中实际上是不可能的
看到您的屏幕截图,您的文档包含一个名为“201”的 属性 类型数组。该数组包含术语“OrderItemModel”对象。我不确定为什么要这样称呼这个数组,因为“orderItems”可能更有意义。但是,要将该数组映射到 List<OrderItemModel>
,您只需创建一个 class 和单个 属性:
class OrdersDocument {
public List<OrderItemModel> orderItems;
}
现在,您可以创建对文档的引用、附加侦听器并将数组转换为自定义对象列表:
List<OrderItemModel> orderItems = document.toObject(OrdersDocument.class).orderItems;
正如我在以下文章中所解释的那样 link:
How to map an array of objects from Cloud Firestore to a List of objects?
谁能告诉我如何从一个应用程序将一组自定义对象存储在 Firebase Firestore (Android) 中,然后将这些对象检索回另一个应用程序(这两个应用程序都是连接到同一个 Firebase 项目)。 我尝试使用地图,我能够存储订单列表,但我无法取回 ArrayList。 这是我的做法:
Map<String, ArrayList<OrderItemModel>> map = new HashMap<>();
map.put(currentOrderNumber, orderList);
firebaseFirestore.collection("orders").document(college).set(map, SetOptions.merge())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "Order placed.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), "Couldn't place order. Please try again.", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
当我尝试使用 documentSnapshot.getData() 检索它时,它 returns Map
public class OrderItemModel {
private String itemName;
private String itemPrice;
private String itemCount;
public OrderItemModel() {
}
public OrderItemModel(String itemName, String itemPrice, String itemCount) {
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemCount = itemCount;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemPrice() {
return itemPrice;
}
public void setItemPrice(String itemPrice) {
this.itemPrice = itemPrice;
}
public String getItemCount() {
return itemCount;
}
public void setItemCount(String itemCount) {
this.itemCount = itemCount;
}
}
我的 Firebase Firestore:
我已经尝试解决这个问题一个星期了,但仍然一无所获。我已经尝试了很多东西,但仍然找不到解决方案。如果有什么不清楚的请询问。请帮助。 谢谢。
编辑:
我正在按以下方式检索数据:
firebaseFirestore.collection("orders").document(college).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
assert documentSnapshot != null;
if (documentSnapshot.exists()) {
Map<String, Object> listMap = documentSnapshot.getData();
for (Map.Entry<String, Object> entry : listMap.entrySet()) {
Log.d(TAG, "onComplete: Key: " + entry.getKey() + ", Value: " + entry.getValue().toString());
if (entry.getKey().equals(currentOrderNumber)) {
orderList.addAll((Collection<? extends OrderItemModel>) entry.getValue());
}
}
Log.d(TAG, "onComplete: ArrayList: " + orderList.get(0).getItemName()); // This line shows error.
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(ShowOrder.this, "Order document not found!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(ShowOrder.this, "An error occurred while fetching the data.", Toast.LENGTH_SHORT).show();
}
}
});
据我了解,我已成功检索到 Map
Log.d(TAG, "onComplete: ArrayList: " + orderList.get(0).getItemName());
上面一行显示如下错误:
2021-01-10 23:48:40.192 8747-8747/com.campuscanteen.main E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.campuscanteen.main, PID: 8747
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.campuscanteen.main.Models.OrderItemModel
at com.campuscanteen.main.ShowOrder.onComplete(ShowOrder.java:89)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:230)
at android.app.ActivityThread.main(ActivityThread.java:7815)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:526)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
编辑 2:
我按照你的建议做了一个class:
public class OrderList {
public List<OrderItemModel> modelList;
public OrderList() {
}
@Override
public String toString() {
return "OrderList{" +
"modelList=" + modelList +
'}';
}
}
并尝试了这些行来检索数据:
List<OrderItemModel> myLists = documentSnapshot.toObject(OrderList.class).modelList;
Log.d(TAG, "onComplete: ArrayList: " + myLists.toString()); // myLists here has null value.
这样列表就有了空值。请原谅我缺乏知识,请帮助。
您收到以下错误:
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.campuscanteen.main.Models.OrderItemModel
因为你试图将 HashMap 类型的对象转换为 OrderItemModel 类型的对象,这在 Java.
中实际上是不可能的看到您的屏幕截图,您的文档包含一个名为“201”的 属性 类型数组。该数组包含术语“OrderItemModel”对象。我不确定为什么要这样称呼这个数组,因为“orderItems”可能更有意义。但是,要将该数组映射到 List<OrderItemModel>
,您只需创建一个 class 和单个 属性:
class OrdersDocument {
public List<OrderItemModel> orderItems;
}
现在,您可以创建对文档的引用、附加侦听器并将数组转换为自定义对象列表:
List<OrderItemModel> orderItems = document.toObject(OrdersDocument.class).orderItems;
正如我在以下文章中所解释的那样 link:
How to map an array of objects from Cloud Firestore to a List of objects?