如何将查询传递给FirestoreRecyclerOptions?

时间:2018-07-28 18:35:41

标签: android firebase google-cloud-firestore firebaseui

我正在使用FirestoreRecyclerAdapter在recyclerView中显示我的物品。 使用searchView,我可以创建查询:

querySearch = FirebaseFirestore.getInstance()
                   .collection("App").document(" " + userID).collection("reminders")
                   .startAt("title", newText)
                   .endAt("title", newText+"\uf8ff");

我的问题是,如何将查询传递给FirestoreRecyclerOptions,以便我的recyclerView显示正在搜索的内容?

(我正在使用Cloud Firestore)

编辑:这是我的更新代码

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
       @Override
       public boolean onQueryTextSubmit(String query) {
           return false;
       }

       @Override
       public boolean onQueryTextChange(String newText) {


           querySearch = FirebaseFirestore.getInstance()
                   .collection("App").document(" " + userID).collection("reminders")
                   .orderBy(newText);


           FirestoreRecyclerOptions<Reminder> options = new FirestoreRecyclerOptions.Builder<Reminder>()
                   .setQuery(querySearch, Reminder.class)
                   .build();


           //create new FirestoreRecyclerAdapter:
           adapterSearch = new FirestoreRecyclerAdapter<Reminder, ReminderHolder>(options) {
               @Override
               public void onBindViewHolder(final ReminderHolder holder, int position, final Reminder model) {

                   holder.bind(model);
                   holder.itemView.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View view) {

                           openEditPopup();

                           DocumentSnapshot snapshot = getSnapshots().getSnapshot(holder.getAdapterPosition());
                           docID = snapshot.getId();

                       }
                   });
               }

               @Override
               public ReminderHolder onCreateViewHolder(ViewGroup group, int i) {
                   View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_beta, group, false); //todo use beta item layout
                   return new ReminderHolder(view);
               }
           };

           recyclerView.setAdapter(adapterSearch);
           adapterSearch.startListening(); //connects to firebase collection
           adapterSearch.notifyDataSetChanged();

1 个答案:

答案 0 :(得分:2)

您需要使用FirebaseUI for Cloud Firestore才能使用类FirestoreRecyclerOptions,并在build.gradle文件中添加以下依赖项:

implementation 'com.google.firebase:firebase-firestore:17.0.4'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.firebaseui:firebase-ui-firestore:4.1.0'

然后您可以执行以下操作:

querySearch = FirebaseFirestore.getInstance()
               .collection("App").document(" " + userID).collection("reminders")
               .startAt("title", newText)
               .endAt("title", newText+"\uf8ff"); 


FirestoreRecyclerOptions<ModelClass.class> options = new FirestoreRecyclerOptions.Builder<ModelClass.class>()
    .setQuery(querySearch, ModelClass.class)
    .build();

还要检查docs