Alex Mamo
Alex Mamo

Reputation: 138969

How to display records from a Firebase database using a condition with Android?

I have the following Firebase database:

enter image description here

All i want to do, is to display all the lists that belong to a specific user. In this case, i want to display List aaa and List bbb that belong to gmail@gmail,com user. With this query:

databaseReference = FirebaseDatabase.getInstance().getReference();

Query query = databaseReference.child("Lists").orderByChild("ListName");
firebaseListAdapter = new FirebaseListAdapter<ListModel>(this, ListModel.class, android.R.layout.simple_list_item_1, query) {
        @Override
        protected void populateView(View v, ListModel slm, int position) {
            ((TextView)v.findViewById(android.R.id.text1)).setText(slm.getListName());
        }
    };

I achieve to display all the lists but i want to display only the lists that belong to a single user and has the value of true. How can i do that?

Thanks in advance!

Upvotes: 0

Views: 3334

Answers (1)

Deepesh
Deepesh

Reputation: 533

There are two ways to achieve this behaviour.

  1. Query query = DatabaseRef.getReference().child("Lists").orderByChild("Users").equals("gmail@gmail,com")

Then add the ChildEventListener to this reference

  1. In Firebase data structure, make the email id parent and add all the childs under it, like

Lists \gmail@gmail,com \listName aaa \listName bbb

You can get data from this structure from below reference:

Query query = DatabaseRef.getInstance().child("Lists").child("gmail@gmail,com")

Then add ChildEventListener listener with your ref object.

Hope this will work for you..

--- Edited ---- Try using this:

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference ref = firebaseDatabase.getReference().child("Lists").orderByChild("Users").equalTo("gmail@gmail,com");

ref.addChildEventListener(New ChildEventListener......);

Upvotes: 1

Related Questions