我将数据放入Firebase文档中
<ion-back-button class="btnmy11" defaultHref="/" [ngClass]="{'back-hide': menuIsHidden==true}"></ion-back-button>
该帖子属于“关注”类别
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();
}
我已宣布出价为TreeMap类型。 但是当我试图像这样从我的应用程序中的Firebase取回数据
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);
}
});
}
说
Post obj = documentSnapshot.toObject(Post.class);
我现在将Post类的出价设置为HashMap,我一直使用TreeMap,但是现在当Firebase的数据来临时,HashMap来了,为什么会这样。
我尝试将TreeMap转换为HashMap并能正常工作,但随后我想使用TreeMap。
任何人都可以向我解释。 谢谢。
答案 0 :(得分:-1)
将其声明为Map,然后尝试使用树作为构造函数的参数创建树形地图对象