我的应用程序中有一个搜索功能,尽管由于firebase firestore数据库查询的原因,它非常有限,但我还是设法找到了与用户输入开头匹配的字符串。
现在一切都正常了,即使我写了一个字符串,比如“starbucks”,然后我一个字符一个字符地慢慢删除它,它会将我的recyclerview重置为满店可用,而不仅仅是starbucks。
但当我快速删除(按下键盘上的后退按钮)时,它会在特定的商店停止,不会刷新。
是什么原因造成的?
这是我的搜索适配器:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchAdapterViewHolder> {
public Context c;
public ArrayList<Shop> arrayList;
public SearchAdapter(Context c,ArrayList<Shop> arrayList){
this.c = c;
this.arrayList = arrayList;
}
@Override
public int getItemCount(){
return arrayList.size();
}
@Override
public long getItemId(int position){
return position;
}
@NonNull
@Override
public SearchAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_item,parent,false);
return new SearchAdapterViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull SearchAdapterViewHolder holder, int position) {
final Shop shop = arrayList.get(position);
holder.list_name.setText(shop.getName());
String removeBraces = shop.getLocation();
String removeLeftBrace = removeBraces.replace("[","");
String removeRightBrace = removeLeftBrace.replace("]","");
holder.list_location.setText(removeRightBrace);
holder.list_overallRating.setText(""+shop.getRatings());
holder.setHeaderImage(shop.getShopHeaderImg());
holder.list_profileImage(shop.getShopProfileImg());
holder.cardLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DetailsActivity.class);
intent.putExtra("shopModel", shop);
context.startActivity(intent);
}
});
}
public class SearchAdapterViewHolder extends RecyclerView.ViewHolder {
private RelativeLayout cardLayout;
private TextView list_name;
private TextView list_location;
private TextView list_overallRating;
private ImageView header_img;
private ImageView list_profileImage;
public SearchAdapterViewHolder(@NonNull View itemView) {
super(itemView);
list_name = itemView.findViewById(R.id.list_name);
list_location = itemView.findViewById(R.id.list_location);
list_overallRating = itemView.findViewById(R.id.list_overallRating);
header_img = itemView.findViewById(R.id.header_img);
list_profileImage = itemView.findViewById(R.id.list_profileImage);
cardLayout = itemView.findViewById(R.id.cardLayout);
}
public void setHeaderImage(final String Image) {
final ImageView headerImg = (ImageView)itemView.findViewById(R.id.header_img);
Picasso.get().load(Image).into(headerImg);
}
public void list_profileImage( final String image) {
final ImageView profileImg = (ImageView)itemView.findViewById(R.id.list_profileImage);
Picasso.get().load(image).into(profileImg);
}
}
}
这是我的addtextchangelistener:
searchShops.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable s) {
if(!s.toString().isEmpty()){
searchInDB(s.toString().toLowerCase());
}else{
searchInDB("");
}
}
});
下面是我的searchindb函数:
private void searchInDB(final String s) {
firebaseFirestore.collection("Shops").orderBy("searchName").startAt(s).endAt(s+"\uf8ff").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
QuerySnapshot querySnapshot = task.getResult();
if(!querySnapshot.isEmpty()){
arrayList.clear();
for(QueryDocumentSnapshot DocumentSnapshot : querySnapshot) {
final Shop shop = DocumentSnapshot.toObject(Shop.class);
arrayList.add(shop);
}
SearchAdapter searchAdapter = new SearchAdapter(getApplicationContext(),arrayList);
recyclerView.setAdapter(searchAdapter);
searchAdapter.notifyDataSetChanged();
}else{
arrayList.clear();
}
}else{
}
}
});
1条答案
按热度按时间yqkkidmi1#
更改:
到