jason_proger
jason_proger

Reputation: 75

Syntax for calling Firebase recyclerview query

I'm creating a query to sort my firebase recyclerview by timestamp in a fragment. I have a syntax question on how to call the query.

Here is a clip of the code I have. I'm unsure about how to write the line "query.addListenerForSingleValueEvent(????)"

public void onStart(){
    super.onStart();

    FirebaseRecyclerOptions<Posts> options =
            new FirebaseRecyclerOptions.Builder<Posts>()
            .setQuery(usersRef, Posts.class)

            .build();

    Query query = FirebaseDatabase.getInstance().getReference("postId").orderByChild("timestamp").limitToFirst(100);

    query.addListenerForSingleValueEvent(????)

    FirebaseRecyclerAdapter<Posts, viewPostsViewHolder> adapter =
            new FirebaseRecyclerAdapter<Posts, viewPostsViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull final viewPostsViewHolder holder, final int position, @NonNull Posts model) {

                    final String postId = getRef(position).getKey();

                    usersRef.child(postId).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {

                            Log.e("heredata","heredata"+dataSnapshot);

                            if (dataSnapshot.exists()) {

Thanks in advance to any help

Upvotes: 1

Views: 56

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

If you're trying to show the query results in the recycler view, you should pass that query into the builder's setQuery(...) call:

Query query = FirebaseDatabase.getInstance().getReference("postId").orderByChild("timestamp").limitToFirst(100);

FirebaseRecyclerOptions<Posts> options =
        new FirebaseRecyclerOptions.Builder<Posts>()
                                   .setQuery(query, Posts.class)
                                   .build();

Upvotes: 1

Related Questions