【问题标题】:How to get random documents from Firestore and display them in different TextViews?如何从 Firestore 获取随机文档并在不同的 TextView 中显示它们?
【发布时间】:2020-01-06 13:54:31
【问题描述】:

在我的应用中,用户应该能够从随机生成的 3 个不同类别(共 8 个)中进行选择。为此,我使用了here 描述的方法。

我不知道如何使用这种方法来使用检索到的文档将它们插入到我的 Category Activity 中的 TextViews 中。我创建了一个类别类来存储从 Firestore 检索到的文档,但我也不知道这是否是正确的方法。我会非常感谢每一个提示!

这是我的类别活动,其中 3 个文档被检索并最终应显示在 TextViews 中:

@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_category_selection, container, false);

        tvCategory1 = view.findViewById(R.id.text_view_category_1);
        tvCategory2 = view.findViewById(R.id.text_view_category_2);
        tvCategory3 = view.findViewById(R.id.text_view_category_3);
        textViewCategoryRound = view.findViewById(R.id.text_view_category_round);

        imageCategory1 = view.findViewById(R.id.image_category_1);
        imageCategory2 = view.findViewById(R.id.image_category_2);
        imageCategory3 = view.findViewById(R.id.image_category_3);

        categoryCollectionRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {

                    List<Category> categoryList = new ArrayList<>();
                    for (DocumentSnapshot document : task.getResult()) {
                        Category category = document.toObject(Category.class);
                        categoryList.add(category);
                    }

                    int categoryListSize = categoryList.size();
                    List<Category> randomCategoryList = new ArrayList<>();
                    for (int i = 0; i < categoryListSize; i++) {
                        Category randomCategory = categoryList.get(new Random().nextInt(categoryListSize));
                        if (!randomCategoryList.contains(randomCategory)) {
                            randomCategoryList.add(randomCategory);
                            if (randomCategoryList.size() == 3) {

                                break;

                            }
                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                        }
                    }

                }
            }
        });

        return view;
    }
}

这是我希望正确存储文档的类别类:

public class Category {

    public String category_name;


    public String picture_id;

    public Category (){

    }


    public String getPicture_id() {
        return picture_id;
    }

    public void setPicture_id(String picture_id) {
        this.picture_id = picture_id;
    }

    public String getCategory_name() {
        return category_name;
    }

    public void setCategory_name(String category_name) {
        this.category_name = category_name;
    }

    public Category(String category_name){
        this.category_name = category_name;
    }
}

这是我构建类别集合的方式(有 8 个文档/类别):

【问题讨论】:

  • 我不明白你卡在问题的哪一部分。
  • 问题是我不知道我是否以正确的方式使用了这两个代码来检索文档。而且我也不知道如何在 CategoryActivity 的 TextViews 中显示文档,但 Numan Turkeri 有正确的方法

标签: android firebase google-cloud-firestore


【解决方案1】:

您好,我可以建议简单的方法。

 List<Category> categoryList =null;

   categoryCollectionRef.get().addOnCompleteListener(new 
   OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {

               categoryList = new ArrayList<>();
                for (DocumentSnapshot document : task.getResult()) {
                    Category category = document.toObject(Category.class);
                    categoryList.add(category);
                }



                }

            }
        }
    });
testButtonClickListener....{
Collections.shuffle(categoryList);
    showShuffledList();
}
public void showShuffledList(){
textView1.setText(categoryList.get(0).getCategoryName())
 textView2.setText(categoryList.get(1).getCategoryName())
 textView3.setText(categoryList.get(2).getCategoryName())
}

当然你应该添加空检查所有步骤并配置更多方式。

【讨论】:

    【解决方案2】:

    你可以做三件事:

    1) 保存文档时,在末尾使用递增数字命名它们,然后在范围内生成 3 个随机数并获取以这些数字结尾的文档。

    2) 在所有文档中添加一个包含唯一编号的字段,并遵循与上述类似的方法,但对这些字段使用查询。

    3) 创建一个特殊的文档,其中包含数组中所有其他文档的名称。然后根据生成的随机数从数组中取出 3 个文档名称。

    可能还有其他方法,但这些是我首先想到的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 2021-11-05
      • 2022-01-02
      • 2020-07-29
      • 2021-05-28
      相关资源
      最近更新 更多