参数1的类型为java.util.TreeMap,得到了java.util.HashMap
我把数据放在Firebase文档中作为参数1的类型为java.util.TreeMap,得到了java.util.HashMap,java,android,firebase,collections,Java,Android,Firebase,Collections,我把数据放在Firebase文档中作为 try { String uid = user.getUid(); String title = InputProductTitle.getText().toString(); Double price = Double.valueOf(InputProductPrice.getText().toString()); Strin
try {
String uid = user.getUid();
String title = InputProductTitle.getText().toString();
Double price = Double.valueOf(InputProductPrice.getText().toString());
String categories = CategoryDataFromPreviousIntent.split("&&")[0];
String subCategories = CategoryDataFromPreviousIntent.split("&&")[1];
String dateAdded = new Date().toString();
String description = InputProductDescription.getText().toString();
Post newPost = new Post(uid,title, price, categories, subCategories, dateAdded, false,description);
mFirebase.collection("NewPost").document().set(newPost).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "Added", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Error" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
我现在把Post类的出价作为HashMap,我总是使用TreeMap,但是现在当来自Firebase的数据到来时,它作为HashMap出现了为什么会发生这种情况
我试着将树映射转换为HashMap,无论它在哪里工作都很好,但是我想使用树映射
谁能给我解释一下吗。
谢谢。将其声明为Map,然后尝试使用Map作为构造函数参数创建树映射对象将其声明为Map,然后尝试使用Map作为构造函数参数创建树映射对象为什么声明为TreeMap而不是HashMap?您是否尝试将其声明为HashMap?你仍然有同样的错误吗?Doug Stevenson,因为我想以某种方式排序。user207421是的,我可以将它声明为映射,但我想知道为什么这会给我带来错误?我尝试了HashMap,一切都很好。为什么要声明为树映射而不是HashMap?您是否尝试将其声明为HashMap?你仍然有同样的错误吗?Doug Stevenson,因为我想以某种方式排序。user207421是的,我可以将它声明为映射,但我想知道为什么这会给我带来错误?我尝试了HashMap,一切都很好。在这种情况下,您不必“解析”,无论这意味着什么,都不要误用标准术语,甚至不要重复使用它。有一个构造器可以做到这一点,你不必“通过解析”,不管这意味着什么,不要误用标准术语,甚至不要重复使用它。有一个构造函数可以实现这一点。
public class Post {
private String addedBy;
private String title;
private Double price;
private String categories;
private String subCategories;
private TreeMap<String,Double> bids;
private String dateAdded;
private boolean status;
private String imageLink;
private String soldTo;
private String description;
public Post(){}
public Post(String addedBy, String title ,Double price, String categories, String subCategories, String dateAdded, boolean status, String description) {
this.addedBy = addedBy;
this.title = title;
this.price = price;
this.categories = categories;
this.subCategories = subCategories;
this.dateAdded = dateAdded;
this.status = status;
this.imageLink = "";
this.soldTo = "";
this.description = description;
this.bids = new TreeMap<String, Double>();
}
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference notebookRef = db.collection("NewPost");
public void loadNotes() {
notebookRef.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Map<String, Object> allData = documentSnapshot.getData();
Post obj = documentSnapshot.toObject(Post.class);
}
});
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ems.erp.mmdu.com.forfirebasestorage, PID: 31905
java.lang.IllegalArgumentException: method ems.erp.mmdu.com.forfirebasestorage.Post.setBids argument 1 has type java.util.TreeMap, got java.util.HashMap
at java.lang.reflect.Method.invoke(Native Method)
at com.google.firebase.firestore.util.ApiUtil.invoke(com.google.firebase:firebase-firestore@@18.0.1:61)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@18.0.1:702)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@18.0.1:675)
at com.google.firebase.firestore.util.CustomClassMapper.convertBean(com.google.firebase:firebase-firestore@@18.0.1:504)
at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-firestore@@18.0.1:243)
at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-firestore@@18.0.1:97)
at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:203)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:121)
at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:183)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:101)
at ems.erp.mmdu.com.forfirebasestorage.AmmarFragment$1.onSuccess(AmmarFragment.java:105)
at ems.erp.mmdu.com.forfirebasestorage.AmmarFragment$1.onSuccess(AmmarFragment.java:98)
at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:200)
at android.app.ActivityThread.main(ActivityThread.java:6956)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:519)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836)