英文:
How to query an array list in a Firebase document?
问题
我正在尝试查询我的数据库以供Android应用程序使用,如果一个用户名在我的数据库的'fave'数组字段中,然后更改图像的背景。我的数据库设置如下...
我不知道我是否做得对,但目前我认为我可能正在检查整个集合,而不是特定的文档,而且它甚至没有返回任何内容。任何建议将不胜感激!
英文:
I'm trying to query my database for an android application, if a username is in the 'fave' array field of my database and if so then change the background of an image. My database is set up like this...
I don't know if i'm doing it right but currently i think i may be checking the whole collection rather than a specific document and it's not even returning anything. Any suggestions would be appreciated!
答案1
得分: 1
FirebaseFirestore.getInstance().collection("Trainers").document("Air Force 1 Low").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
List<String> list = (List<String>) documentSnapshot.get("fave");
for (String name : list){
if (name.equals("Admin")){
// 如果用户是管理员则执行以下操作
return;
}
}
// 如果用户不是管理员则执行其他操作
}
});
英文:
You wanted this?
FirebaseFirestore.getInstance().collection("Trainers").document("Air Force 1 Low").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
List<String> list = (List<String>)documentSnapshot.get("fave");
for (String name : list){
if (name.equals("Admin")){
//do if user is admin
return;
}
}
//do if user is not admin
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论