【发布时间】: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;
}
}
【问题讨论】:
-
我不明白你卡在问题的哪一部分。
-
问题是我不知道我是否以正确的方式使用了这两个代码来检索文档。而且我也不知道如何在 CategoryActivity 的 TextViews 中显示文档,但 Numan Turkeri 有正确的方法
标签: android firebase google-cloud-firestore