我正在尝试在firestore集合中实现搜索功能,该功能应该在firestore回收器视图中获取并显示包含输入字符串的所有文档,但是我的回收器视图始终为空并且不返回任何项目。 请看一下我的代码: //此方法会将数据加载到回收站视图中
private void initializeData()
{
db = FirebaseFirestore.getInstance();
//Query query = null;
Query query;
if (txt!=null){
query = db.collection("Items").whereEqualTo(txt, true);
}else{
query = db.collection("Items");
s="Items";
}
FirestoreRecyclerOptions<FriendsResponse> response = new FirestoreRecyclerOptions.Builder<FriendsResponse>()
.setQuery(query, FriendsResponse.class)
.build();
adapter = new FirestoreRecyclerAdapter<FriendsResponse, FriendsHolder>(response) {
@Override
public void onBindViewHolder(FriendsHolder holder, int position, FriendsResponse model) {
String id = getSnapshots().getSnapshot(position).getId();
holder.exname.setText(id);
db.collection("Items").document(id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
DocumentSnapshot document = task.getResult();
String url= (String) document.getData().get("imageurl");
// Toast.makeText(getContext(),url,Toast.LENGTH_LONG).show();
Glide.with(getContext()).load(url).into(holder.imgmenu);
}
});
}
@Override
public FriendsHolder onCreateViewHolder(ViewGroup group, int i) {
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.menu_item, group, false);
return new FriendsHolder(view);
}
@Override
public void onError(FirebaseFirestoreException e) {
Log.e("error", e.getMessage());
}
};
adapter.notifyDataSetChanged();
//int spaceInPixels = 200;
DividerItemDecoration verticalDecoration = new DividerItemDecoration(recyclerView.getContext(),
DividerItemDecoration.HORIZONTAL);
Drawable verticalDivider = ContextCompat.getDrawable(getActivity(), R.drawable.divider_vertical);
verticalDecoration.setDrawable(verticalDivider);
recyclerView.addItemDecoration(verticalDecoration);
DividerItemDecoration horizontalDecoration = new DividerItemDecoration(recyclerView.getContext(),
DividerItemDecoration.VERTICAL);
Drawable horizontalDivider = ContextCompat.getDrawable(getActivity(), R.drawable.divider_horizontal);
horizontalDecoration.setDrawable(horizontalDivider);
recyclerView.addItemDecoration(horizontalDecoration);
recyclerView.setAdapter(adapter);
}
这是我的搜索栏的代码:
public void search(){
//setContentView(R.layout.view_feed_toolbar);
searchBar = (MaterialSearchBar) getActivity().findViewById(R.id.searchBar);
searchBar.setSpeechMode(false);
searchBar.enableSearch();
searchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
@Override
public void onSearchStateChanged(boolean enabled) {
String s = enabled ? "enabled" : "disabled";
Toasty.info(getContext(), "Search " + s, Toast.LENGTH_SHORT,true).show();
if (s.equals("disabled")){
searchBar.setVisibility(View.INVISIBLE);
txt=null;
initializeData();
}
}
@Override
public void onSearchConfirmed(CharSequence text) {
txt=text.toString();
initializeData();
}
@Override
public void onButtonClicked(int buttonCode) {
}
});
}