gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:15:31 24 4
gpt4 key购买 nike

我知道如何在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com