0
votes

I am developing a team chat application . I am storing chat data in firebase.

This is my Data structure enter image description here

  • The chats are stored under a node named chats.
  • each immediate child of chats is the name of a team(like whatsapp.. chat groups).
  • the team node have all chat messages. the chat messages are stored
  • with a parent node in the format timestamp_chat

My Problem

Now I have to create a firebase query to fetch this team nodes by from key.

That is if a from: field contains the a given search value (say, "ragesh"), then the parent node "team_name" have to be returned(say,"teamrova") as a datasnapshot.

Simply speaking , I want datasnapshot of teams that have a particular username in the from field.

My Work


I tried the follwing code :

  reference1 = FirebaseDatabase.getInstance().getReference().child("chats");

    Query chatQuery = reference1.orderByChild("from").equalTo("ragesh");
    System.out.println("Chat list :: starting");

    chatQuery.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            System.out.println("Chat list :: onChildAdded");

            for (DataSnapshot chat : dataSnapshot.getChildren()) {

                HashMap<String, Object> hashMap = (HashMap<String, Object>) dataSnapshot.getValue();
                System.out.println("Chat list :: The chat group is =>" + dataSnapshot.getKey());
                System.out.println("Chat list :: The key & Value => " + chat.getKey() +" :: "+chat.getValue().toString());

            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(ChatListActivity.this, "Connection Error "+databaseError.getDetails(), Toast.LENGTH_SHORT).show();
            System.out.println("Chat list :: databaseError "+databaseError.toString());


        }
    }); 

Sadly , above code don't return any value. even the
System.out.println("Chat list :: onChildAdded"); not called. And it doesn't calls onCancelled also.(so,no error).

I get sturcked by this. please help me.

1

1 Answers

2
votes

To get the username:

mRootRef.child("chats").child("teamrova").orderByChild("from").equalTo("ragesh").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
   for(DataSnapshot datas: dataSnapshot.getChildren()){
     String name=datas.child("from").getValue().toString();



}

@Override
public void onCancelled(DatabaseError databaseError) {

   }
});

Since you are using addChildeventListener then you do not need the for loop to be able to get those values for (DataSnapshot chat : dataSnapshot.getChildren()) {

You also need to change this:

 reference1 = FirebaseDatabase.getInstance().getReference().child("chats");

to this:

reference1 = FirebaseDatabase.getInstance().getReference().child("chats").child("teamrova");

The above code is an alternative with addValueEventListener