希望你一切都好。我正在尝试将文本从 Firestore 中的文档添加到 TextView 。有人告诉我不能通过集合组中的 id 查询文档,因此我使用 documentID 创建了一个字段“id”并对其进行了查询,但仍然没有任何结果,也没有错误。
这是我的 Activity :
public class FoodItemPage extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
ElegantNumberButton number_button;
Button addtocart;
TextView food_name, food_description;
EditText extra_notes;
String foodId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_item_page);
foodId = getIntent().getStringExtra("foodid");
//firestore
Query singlefoodref = db.collectionGroup("Foods").whereEqualTo("id", foodId);
Log.d(TAG, "well hello there" + foodId);
//init view
number_button = (ElegantNumberButton) findViewById(R.id.number_button);
addtocart = (Button) findViewById(R.id.addToCart);
food_name = (TextView) findViewById(R.id.food_name1);
food_description = (TextView) findViewById(R.id.food_description);
singlefoodref.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if (queryDocumentSnapshots.isEmpty()){
Toast.makeText(FoodItemPage.this, "document not there", Toast.LENGTH_SHORT);
Log.d(TAG, "NOT WORKING");
}else{
FoodModel foodModel = queryDocumentSnapshots.getDocuments().get(0).toObject(FoodModel.class);
food_name.setText(foodModel.getName());
Log.d(TAG, "WORKING");
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(FoodItemPage.this, "Error", Toast.LENGTH_SHORT);
}
});
}
}
我的模型(我也在另一个 Activity 中使用相同的模型,这应该不是问题,对吧?):
package com.example.hostapp.Models;
public class FoodModel {
private String name;
private String description;
private String image;
private String price;
private String discount;
private String categoryid;
public FoodModel() {
}
public FoodModel(String name, String description, String image, String price, String discount, String categoryid) {
this.name = name;
this.description = description;
this.image = image;
this.price = price;
this.discount = discount;
this.categoryid = categoryid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getDiscount() {
return discount;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public String getCategoryid() {
return categoryid;
}
public void setCategoryid(String categoryid) {
this.categoryid = categoryid;
}
}
编辑: 我的数据库结构,每个餐厅都有自己的“食品”集合,其中包含我尝试加载的食品。
最佳答案
所以我修复了这个问题,错误出现在我的数据库中,数据库中的 ID 中有一个空格,因此它与上一个 Activity 的文档 ID 不匹配。
所以从“123456”到“123456”
关于java - collectionGroup 文档中的数据未加载到 TextView Firestore 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61612831/