Kmel
Kmel

Reputation: 285

FirestoreRecyclerAdapter not updated after new firestore query

I have a FloatingSearchView in my app to perform some query on my Firestore database. When I check the size of each query, the result is as expected but my view is not updating with the result. I don't understand if this is the queries or if this is how I handle the different adapter.

I have one FirestoreRecyclerAdapter for each query. I don't understand what's wrong. Thank you for your help!

    floatingSearchView.setOnSearchListener(new 
       FloatingSearchView.OnSearchListener() {


        @Override
                    public void onSuggestionClicked(SearchSuggestion 
           searchSuggestion) {

           mLastQuery = searchSuggestion.getBody();

                    com.google.firebase.firestore.Query qSuggestion =
                            db
                                    .collection("article")
                                    .whereEqualTo("category", category_key)
                                    .whereEqualTo("type", mLastQuery);

                    qSuggestion
                            .get()
                            .addOnSuccessListener(new 
        OnSuccessListener<QuerySnapshot>() {
                                @Override
                                public void onSuccess(QuerySnapshot 
        documentSnapshots) {
                                    int size = documentSnapshots
                                            .getDocuments()
                                            .size();
                                    Toast.makeText(Blog.this, "size " + size, 
        Toast.LENGTH_LONG).show();
                                }
                            });

                    FirestoreRecyclerOptions<Blog_model> opt = new FirestoreRecyclerOptions.Builder<Blog_model>()
                            .setQuery(qSuggestion, Blog_model.class)
                            .build();

                    Log.d("option", opt.getSnapshots().toString());

                    suggestionAdapter = new FirestoreRecyclerAdapter<Blog_model, Blog.BlogViewHolder>(opt) {
                        @Override
                        public void onBindViewHolder(@NonNull Blog.BlogViewHolder holder, int position, @NonNull final Blog_model model) {
                            holder.setTitle(model.getTitle());
                            holder.setDesc(model.getDesc());
                            holder.setImage(getApplicationContext(), model.getPicture());

                            final String post_key = getSnapshots().getSnapshot(position).getId();

                            holder.mView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    // Ordinary Intent for launching a new activity
                                    final Intent intent = new Intent(Blog.this, BlogDetails.class);
                                    intent.putExtra("article_id", post_key);
                                    intent.putExtra("category_key", category_key);
                                    intent.putExtra("image", model.getPicture());
                                    intent.putExtra("title", model.getTitle());

                                    startActivity(intent);
                                }
                            });
                        }

                        @Override
                        public Blog.BlogViewHolder onCreateViewHolder(ViewGroup group, int i) {
                            // Create a new instance of the ViewHolder, in this case we are using a custom
                            // layout called R.layout.message for each item
                            View view = LayoutInflater.from(group.getContext())
                                    .inflate(R.layout.blog_row, group, false);

                            return new Blog.BlogViewHolder(view);
                        }
                    };

                    floatingSearchView.clearSearchFocus();

                    mBlogList.setAdapter(suggestionAdapter);

                }

                @Override
                public void onSearchAction(String currentQuery) {

                    mLastQuery = currentQuery;

                    // query to firebase

                    com.google.firebase.firestore.Query qSuggestion =
                            db
                                    .collection("article")
                                    .whereEqualTo("keyword."+mLastQuery, true);

                    FirestoreRecyclerOptions<Blog_model> options1 = new FirestoreRecyclerOptions.Builder<Blog_model>()
                            .setQuery(qSuggestion, Blog_model.class)
                            .build();

                    Log.d("option", options1.getSnapshots().toString());

                    searchAdapter = new FirestoreRecyclerAdapter<Blog_model, Blog.BlogViewHolder>(options1) {
                        @Override
                        public void onBindViewHolder(@NonNull Blog.BlogViewHolder holder, int position, @NonNull final Blog_model model) {
                            holder.setTitle(model.getTitle());
                            holder.setDesc(model.getDesc());
                            holder.setImage(getApplicationContext(), model.getPicture());

                            final String post_key = getSnapshots().getSnapshot(position).getId();

                            holder.mView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
    //                        Toast.makeText(Blog.this, "title", Toast.LENGTH_LONG).show();

                                    // Ordinary Intent for launching a new activity
                                    final Intent intent = new Intent(Blog.this, BlogDetails.class);
                                    intent.putExtra("article_id", post_key);
                                    intent.putExtra("category_key", category_key);
                                    intent.putExtra("image", model.getPicture());
                                    intent.putExtra("title", model.getTitle());

                                    startActivity(intent);
                                }
                            });
                        }

                        @Override
                        public Blog.BlogViewHolder onCreateViewHolder(ViewGroup group, int i) {
                            // Create a new instance of the ViewHolder, in this case we are using a custom
                            // layout called R.layout.message for each item
                            View view = LayoutInflater.from(group.getContext())
                                    .inflate(R.layout.blog_row, group, false);

                            return new Blog.BlogViewHolder(view);
                        }
                    };

                    mBlogList.setAdapter(searchAdapter);
                }
            });


   @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
        if(suggestionAdapter != null){
            suggestionAdapter.startListening();
        }
        if(searchAdapter != null){
            searchAdapter.startListening();
        }

    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
        if(suggestionAdapter != null){
            suggestionAdapter.stopListening();
        }
        if(searchAdapter != null){
            searchAdapter.stopListening();
        }
    }

Upvotes: 1

Views: 853

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

You are using two FirestoreRecyclerAdapter objects, which is correct, but the problem in your code is that you are not listening to the second adapter for changes in the right place. To solve this, add inside onSearchAction method:

searchAdapter.startListening();

Right after you create the adapter object. This means that for every character that you type in your FloatingSearchView, you create a new adapter and you populate it with the results that are coming from the database. If you are starting listening in the onStart method, it doesn't help you at all.

Upvotes: 1

Related Questions