Java 从 FireBase (Android) 检索特定数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27187725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving Specific Data from FireBase (Android)
提问by engico
I am making a chat app, and I have a Firebase with following Structure. Chat1,Chat2,Chat3... is used for saving data of different Chat groups. Message1,Message2,Message3 are Messages inside 1st group., It can be from any author.
我正在制作一个聊天应用程序,并且我有一个具有以下结构的 Firebase。Chat1,Chat2,Chat3... 用于保存不同聊天组的数据。Message1,Message2,Message3 是第一组内的消息。它可以来自任何作者。
Chats
聊天
- Chat1
- Message1
- Author1
- MessageText
- Message2
- Author2
- MessageText
- Message3
- Author1
- MessageText
- Message1
- Chat2
- Message1
- Author3
- MessageText
- Message2
- Author4
- MessageText
- Message3
- Author1
- MessageText
- Message1
- Chat3
- 聊天 1
- 消息 1
- 作者 1
- 消息文本
- 消息 2
- 作者2
- 消息文本
- 消息 3
- 作者 1
- 消息文本
- 消息 1
- 聊天 2
- 消息 1
- 作者3
- 消息文本
- 消息 2
- 作者4
- 消息文本
- 消息 3
- 作者 1
- 消息文本
- 消息 1
- 聊天 3
I want to retrieve the list of all Messages from "Author 1". Meaning Message1,Message3 should be retrieved from Chat1 and Message3 also should get retrieved from Chat2. I want to display all messages from 1 author.
我想从“作者 1”中检索所有消息的列表。含义 Message1,Message3 应该从 Chat1 中检索,而 Message3 也应该从 Chat2 中检索。我想显示来自 1 个作者的所有消息。
ref = new Firebase(FIREBASE_URL); //Root URL
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
for (DataSnapshot childd : child.getChildren()) {
//This might work but it retrieves all the data
}
}
}
@Override
public void onCancelled() {
}
});
This Gives me entire data! How do I restrict it to "Author1"?
这给了我完整的数据!如何将其限制为“Author1”?
回答by Frank van Puffelen
Firebase will always retrieve the complete node(s) that you ask for. Since you're asking for all Chats or an entire Chat at once (it isn't clear which one you do from your code), that's what you get.
Firebase 将始终检索您要求的完整节点。由于您一次请求所有聊天或整个聊天(从代码中不清楚您做了哪一个),这就是您得到的。
If you want to get less data from Firebase, you'll have to be more specific in what you ask from it. For example if you want to display Chat1, then start by listening for the childrenof the Chat1 node.
如果您想从 Firebase 获取更少的数据,则必须更具体地说明您从中询问的内容。例如,如果你想显示客服1,然后通过监听开始孩子们的客服1的节点。
This snippet comes from the Firebase guide for Android:
此代码段来自适用于 Android的Firebase 指南:
// Retrieve new posts as they are added to Firebase
ref.addChildEventListener(new ChildEventListener() {
// Retrieve new posts as they are added to Firebase
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
Map<String, Object> newPost = (Map<String, Object>) snapshot.getValue();
System.out.println("Author: " + newPost.get("author"));
System.out.println("Title: " + newPost.get("title"));
}
//... ChildEventListener also defines onChildChanged, onChildRemoved,
// onChildMoved and onCanceled, covered in later sections.
});
This snippet accomplishes the same as your current example, but instead of your code looping over the data with a for
, Firebase feeds you the data one message at a time.
此代码段与您当前的示例完成相同的工作,但不是您的代码使用 循环遍历数据,而是for
Firebase 一次向您提供一条消息。
With that in place, you can filter the messages. This is a two-step process: first you order by a child node, then you limit which nodes are returned.
有了它,您就可以过滤消息。这是一个两步过程:首先按子节点排序,然后限制返回的节点。
Query query = ref.orderByChild("Author").equalTo("Author1", "Author");
query.addChildEventListener(new ChildEventListener() {
// the rest of the code remains the same as above
This last bit is best explained in the Firebase documentation on queries.
最后一点最好在Firebase 文档查询中进行解释。
回答by Frank van Puffelen
I don't know if this is what you meant or not, but here is my code for my database. First here is my database structure:
我不知道这是否是您的意思,但这是我的数据库代码。首先是我的数据库结构:
*Root of Database
*数据库根
:Users
:用户
:
:
:....username1
:.. 用户名1
: :
: :
: :......"Name":"value"
: :......"名称":"值"
: :......"Email":"value"
::......“电子邮件”:“价值”
: :......"Password":"value"
: :......"密码":"值"
:
:
:....username2
:.. 用户名2
: :
: :
: :......"Name":"value"
: :......"名称":"值"
: :......"Email":"value"
::......“电子邮件”:“价值”
: :......"Password":"value"
: :......"密码":"值"
Here is my code for retrieve the data from database:
这是我从数据库中检索数据的代码:
FirebaseDatabase database= FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference().child("Users");
login=myRef.child(name); //here user will sign in using his name so it is based on user, directory will change dynamically
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
String demo=user.Email;
//email fetched from database
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
login.addValueEventListener(listener);
Here the User.java class contains 3 String values of email,password & name.
这里 User.java 类包含电子邮件、密码和名称的 3 个字符串值。
public class User {
public String Name;
public String Email;
public String Password;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String name,String pass, String email) {
this.Name = name;
this.Email = email;
this.Password = pass;
}
}
回答by Pramod Garg
You can use the Query to filter the results
您可以使用 Query 过滤结果
DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference("Chat1");
Query query = mDatabaseReference.orderByChild("authorName").equalTo("Author1");
Additionally use can other filter methods given here: https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query
另外可以使用这里给出的其他过滤方法:https: //firebase.google.com/docs/reference/android/com/google/firebase/database/Query