java - FirestoreRecyclerAdapter 在新的 firestore 查询后未更新

标签 java android firebase google-cloud-firestore

我的应用程序中有一个 FloatingSearchView,用于对我的 Firestore 数据库执行一些查询。当我检查每个查询的大小时,结果符合预期,但我的 View 没有随结果更新。我不明白这是查询还是我处理不同适配器的方式。

对于每个查询,我都有一个 FirestoreRecyclerAdapter。我不明白怎么了。感谢您的帮助!

    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();
        }
    }

最佳答案

您正在使用两个 FirestoreRecyclerAdapter 对象,这是正确的,但您代码中的问题是您没有在正确的位置监听第二个适配器的变化。要解决这个问题,请在 onSearchAction 方法中添加:

searchAdapter.startListening();

在您创建适配器对象之后。这意味着对于您在 FloatingSearchView 中键入的每个字符,您都会创建一个新的适配器并使用来自数据库的结果填充它。如果您在 onStart 方法中开始收听,它对您根本没有帮助。

关于java - FirestoreRecyclerAdapter 在新的 firestore 查询后未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49584886/

相关文章:

javascript - 我无法在 javascript 中从 firebase 中获取数据

java - Spring以什么顺序获取构造函数参数中的值

android - 如何关闭 TextView 中数字的突出显示?

ios - 如何在 Firebase 中退出应用程序后保持用户 session

android - GestureDetectorCompat 对事件没有反应

android - 在什么情况下内容解析器返回空游标?

ios - 如何编辑 UITableViewController

java - spring 容器 XML 配置引用可以使用另一个 bean'property

java - 上传 .pdf、.doc 或 .docx 时出现 HTTP 错误。使用 primefaces 上传工具上传文件

java - 监听某些变化并将其更新到数据库