FirestoreRecyclerAdapter在新的firestore查询后未更新

时间:2018-03-31 06:21:55

标签: java android firebase google-cloud-firestore

我的应用中有一个FloatingSearchView来对我的Firestore数据库执行一些查询。当我检查每个查询的大小时,结果是预期的,但我的视图没有更新结果。我不明白这是否是查询,或者这是我处理不同适配器的方式。

每个查询都有一个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();
        }
    }

1 个答案:

答案 0 :(得分:1)

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

searchAdapter.startListening();

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