java - collectionGroup 文档中的数据未加载到 TextView Firestore 中

标签 java android google-cloud-firestore

希望你一切都好。我正在尝试将文本从 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;
    }
}

编辑: 我的数据库结构,每个餐厅都有自己的“食品”集合,其中包含我尝试加载的食品。

enter image description here

最佳答案

所以我修复了这个问题,错误出现在我的数据库中,数据库中的 ID 中有一个空格,因此它与上一个 Activity 的文档 ID 不匹配。

所以从“123456”到“123456”

关于java - collectionGroup 文档中的数据未加载到 TextView Firestore 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61612831/

相关文章:

java - 400 Bad request 在 Spring Controller 中将嵌套 json 对象映射到 java.util.Map 时出错

java - Android:在 ActionBarSherlock 项目中的 Android 2.3 上,按钮的 findViewById 返回 null

java - 删除字符串中引号之间的注释

Java Spring Web - 使用 validator 将子属性转换为对象

android - 使用 ACTION_SEND Intent 附加图像

database - 在 Firestore 中查询没有双子的不同顺序的元素

Firebase/Firestore 用户访问权限和角色如何运作?

java - 如何初始化 Cloud Firestore Bean 以在所有包中使用?

java - 更新(来自 p :ajax) Primefaces datatable select all rows of the table

android - 将图像 URI 传递给另一个 Activity