java - 使用 Firestore 集合中的图像填充 RecyclerView

标签 java android firebase google-cloud-firestore

我知道如何在 Firebase 实时数据库中执行此操作,但由于我只尝试实现 Firestore,我陷入了进退两难的境地。

问题是:在 Firestore 中有办法做到这一点吗?

 final ArrayList<Photo> photos = new ArrayList<>();

  DatabaseReference reference = 
  FirebaseDatabase.getInstance().getReference();
  Query query = reference
            .child("user_photos")
            .child(userID);

  query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for ( DataSnapshot singleSnapshot :  dataSnapshot.getChildren()){
                photos.add(singleSnapshot.getValue(Photo.class));
   }

最佳答案

是的。假设您在 Firestore 中有一个如下所示的数据库结构:

Firestore-root
    |
    --- user_photos (collection)
           |
           --- userID (document)
                 |
                 --- //document details

获取 user_photos 集合中所有文档的代码应该如下所示:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference userPhotoRef = rootRef.collection("user_photos");
userPhotoRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            List<Photo> list = new ArrayList<>();
            for (DocumentSnapshot document : task.getResult()) {
                Log.d(TAG, document.getId() + " => " + document.getData());

                if (document.exists()) {
                    Photo photo = document.toObject(Photo.class);
                    list.add(photo); //Add Photo object to the list
                }

                //Do what you need to do with your list
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

请在此处查看有关 getting data from Cloud Firestore 的更多信息.

关于java - 使用 Firestore 集合中的图像填充 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52594709/

相关文章:

java - 上下文初始化失败 java.lang.IllegalArgumentException

android - 在 IntelliJ IDEA 11 中编译期间未检测到模块依赖性

reactjs - React 和 Firebase Firestore V9 上一页分页返回到 "first page"

Android + Firebase 身份验证 + REST API : How to properly work with token?

firebase - 如何检查Firebase身份验证中是否存在给定电子邮件?

java - 是否有任何类似于 HashMap 的数据结构,我可以在其中添加重复键

java - 指定是否延迟加载 Spring Data

android - 如何从 AlertDialog 获取 View ?

java - 如何在@WebMvcTest测试中忽略@EnableWebSecurity注释的类

android - .obj 到 .glb 转换