JDoe
JDoe

Reputation: 221

Android - Multiple queries for recyclerview (Firebase)

I am working with recylerviews and Firebase. I am using FirebaseUI to populate data to my recyclerview. I was wondering if it was possible to use two queries within my fragment. The query that should be executed should be dependent of if a node in the database exists.

Database Structure:

enter image description here

If the address child is present in the users node, my fragment should query the users node. If not it should query the routes node. Is this possible?

Basically here I am making by query which gets me all markers:

Query keyQuery = FirebaseDatabase.getInstance().getReference(sharedPreferences.getString("school", null)).child("markers");
    recyclerView = (RecyclerView) rootView.findViewById(R.id.markerRecyclerview);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    FirebaseRecyclerOptions<FirebaseMarker> options = new FirebaseRecyclerOptions.Builder<FirebaseMarker>()
            .setQuery(keyQuery, FirebaseMarker.class)
            .build();

Inside my onBindViewHolder method I have an onClick on each item in the recyclerview. When the item is clicked, the user goes to a new activity. In this new activity the user can press a button which will add the address node under the users/userId node. What pressing that button means is that the user have chosen that marker. So I only want to show that marker information in the recyclerview and not every marker in the database.

 holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(),MainActivity.class);
                    intent.putExtra("databaseKey", key);
                    startActivity(intent);
                }
            });

I was thinking that: If a address and time node was inserted in the users node, I could just query this, which would make it easier.

Upvotes: 4

Views: 3134

Answers (1)

Grimthorr
Grimthorr

Reputation: 6926

This is possible, although I think you'd first need to check whether the user has selected a marker and then decide which query to attach to the FirebaseRecyclerAdapter.

To do that, it would likely be necessary to add a list of users that have selected each marker under the marker nodes, something like:

users
  userId
    selectedMarker      // the ID of the marker selected by this user
    ...
markers
  markerId
    ...
    selectedUsers       // list of user IDs that have selected this marker

Then, if the users node contains a selectedMarker value, you could use the below query to get all markers selected by this specific user:

Query query = FirebaseDatabase.getInstance().getReference()
        .child(schoolId).child("markers")
        .orderByChild("selectedUsers/"+userId).equalTo(true);

Where schoolId is from your sharedPreferences.getString("school", null) and userId is the currently logged in user's unique ID.

To check if the user has selected a marker, could be as simple as:

FirebaseDatabase.getInstance().getReference()
        .child(schoolId).child("users").child(userId)
        .addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.child("selectedMarker").exists()) {
                    // Attach above query to FirebaseRecyclerAdapter
                } else {
                    // Attach markers reference (no query) to FirebaseRecyclerAdapter
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });

However, if you just want to display the user's single selected marker, it's likely that you don't need to use the RecyclerView at all, and could just use a separate view to display details about the user's selected marker.

Upvotes: 3

Related Questions