如果另一个集合中的另一个值为 true,则从 Firebase 获取值

我目前在我的 Firebase 实时数据库中有一个 users 集合,其中包含我的所有用户和一个包含书籍名称和评级(竖起和竖起拇指的次数)的集合。 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 我正在尝试制作一个可以不断显示书名(或我的数据库中的书名)的应用程序,以便人们可以对它们进行评分,但是我需要能够检查用户是否已经查看过这本书的书名(这是我需要帮助的部分)。有人可以帮助我从 Firebase 中提取数据并进行比较以查看用户是否查看过这本书的书名。这就是我的数据库设置的样子

http://img3.mukewang.com/60fa79f10001ab5605020522.jpg

繁星点点滴滴
浏览 149回答 2
2回答

月关宝盒

要解决这个问题,请使用以下方法来检查用户是否看过一本书:private void checkBookTitle(String bookTitle) {&nbsp; &nbsp; String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();&nbsp; &nbsp; DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();&nbsp; &nbsp; DatabaseReference ref = rootRef.child("Users").child(uid).child("books_viewed");&nbsp; &nbsp; ValueEventListener valueEventListener = new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(dataSnapshot.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Map.Entry<String, Object> entry : map.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (entry.getValue().equals(bookTitle)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "User has viewed this book");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "User has not viewed this book");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError databaseError) {}&nbsp; &nbsp; };&nbsp; &nbsp; ref.addListenerForSingleValueEvent(valueEventListener);}

牧羊人nacy

首先,books_viewed变成一个数组。阅读本文以了解Firebase 实时数据库中的数组现在它是一个数组,你可以ArrayList<String>在你的应用程序中获取它。public class User {&nbsp; &nbsp; public ArrayList<String> booksViewed;&nbsp; &nbsp; ... // Other variables}然后只需使用该contains功能检查您需要查找的书是否在该列表中DatabaseReference userRef = FirebaseDatabase.getInstance().getReference().child("Users").child("<UserID>")userRef.addValueEventListener(new ValueEventListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onDataChange(DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; User user = dataSnapshot.getValue(User.class);&nbsp; &nbsp; &nbsp; &nbsp; String bookTitleYouAreChecking = "Twilight"&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (user.booksViewed.contains(bookTitleYouAreChecking)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // User has viewed&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // User has not viewed&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCancelled(DatabaseError databaseError) {&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java