英文:
Data from collectionGroup document not loading into TextView Firestore
问题
以下是您要翻译的内容:
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);
}
});
}
}
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;
}
}
编辑:
我的数据库结构,每个餐厅都有自己的 "Foods" 集合,其中包含我要加载的食物项。
英文:
I hope you are well. I am trying to add text to a text view from a document in Firestore. I was told you cant query a document by id withing a collection group, so I created a field "id" with the documentID and queried that, but still nothing and no errors.
Here is my 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);
}
});
}
}
My model (i am also using the same Model in another activity, that shouldn't be a problem right?) :
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;
}
}
Edit:
My database structure, each restaurant has its own "Foods" collections with the food items i am trying to load.
答案1
得分: 0
所以我修复了这个问题,错误出现在我的数据库中,数据库中的ID有一个空格,因此它与之前活动中的文档ID不匹配。
所以从" 123456"变为"123456"。
英文:
SO I fixed this, the error was in my database, the id in the database had a space in it so it wasn't matching the document ID from the previous activity.
so from " 123456" to "123456"
专注分享java语言的经验与见解,让所有开发者获益!
评论