Getting specific data from FireBase (Android)

I am creating a chat application and I have Firebase with the following structure. Chat1, Chat2, Chat3 ... is used to save data from different chat groups. Message1, Message2, Message3 are messages within the 1st group. It can be any author.

Chats

  • Chat1
    • Message1
      • Author1
      • MessageText
    • message2
      • Author2
      • MessageText
    • Message3
      • Author1
      • MessageText
  • Chat2
    • Message1
      • Author3
      • MessageText
    • message2
      • Author4
      • MessageText
    • Message3
      • Author1
      • MessageText
  • Chat3

I want to get a list of all posts from "Author 1". The value of Message1, Message3 must be obtained from Chat1, and Message3 must also be obtained from Chat2. I want to display all posts from 1 author.

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 the whole data! How do I limit it to "Author1"?

+4
3

Firebase node (), . (, ), , .

Firebase, , . , 1, Chat1 node.

Firebase guide Android:

// 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.
});

, , , for, Firebase .

. : node, , .

Query query = ref.orderByChild("Author").equalTo("Author1", "Author");
query.addChildEventListener(new ChildEventListener() {
    // the rest of the code remains the same as above

Firebase .

+10

DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference("Chat1");
   Query query =  mDatabaseReference.orderByChild("authorName").equalTo("Author1");

, : https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query

+1

, , , . , :

*

:

:

:.... username1

::

::...... "Name": "value"

:: ...... "Email": "value"

:: ...... "Password": "value"

:

: .... username2

::

:: ...... "Name": "value"

:: ...... "Email": "value"

:: ...... "Password": "value"

Here is my code to retrieve data from a 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 ​​for email, password, and name.

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;
}
}
0
source

All Articles