英文:
why does my recyclerview not reset when i delete text fast in my searchbar android?
问题
我在我的应用中有一个搜索功能,尽管由于 Firebase Firestore 数据库查询的限制,它的功能非常有限,但我设法使它能够根据用户输入的开头找到匹配的字符串。
现在一切都运行正常,甚至当我输入一个字符串,例如 "starbucks",然后逐个字符地删除它时,我的 RecyclerView 会重置为显示所有可用的商店,而不仅仅是星巴克。
但是当我快速删除(快速按下键盘上的返回按钮)时,它会停留在特定的商店,并且不会刷新。
是什么原因导致了这个问题?
以下是我的搜索适配器:
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;
}
// ... (省略其他方法的内容)
public class SearchAdapterViewHolder extends RecyclerView.ViewHolder {
// ... (省略视图绑定的内容)
public SearchAdapterViewHolder(@NonNull View itemView) {
super(itemView);
// ... (省略视图绑定的内容)
}
// ... (省略图片加载的方法)
}
}
这是我的 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 {
}
}
});
}
英文:
i have a search functionality in my app and although is it very limited due to the firebase firestore database queries, i managed to make it work to find string that match the beginning of the user's input.
now everything works fine, even when i write a string for instance "starbucks" and i slowly delete it character by character it resets my recyclerView to full shops available and not only starbucks.
but when i delete quickly (press down on the back button in the keyboard), it stops at that specific shop and doesnt refresh.
what is causing this?
here is my search adapter:
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);
}
}
}
here is my 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("");
}
}
});
and here is my searchInDB function:
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
得分: 1
Change:<br>
如果(!s.toString().isEmpty()){
在数据库中搜索(s.toString().toLowerCase());
}否则{
在数据库中搜索("");
}
至
如果(!s.toString().isEmpty()){
在数据库中搜索(s.toString().toLowerCase());
}否则{
recyclerView.setAdapter(null);
}
<details>
<summary>英文:</summary>
Change:<br>
if(!s.toString().isEmpty()){
searchInDB(s.toString().toLowerCase());
}else{
searchInDB("");
}
To
if(!s.toString().isEmpty()){
searchInDB(s.toString().toLowerCase());
}else{
recyclerView.setAdapter(null);
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论