【问题标题】:why does my recyclerview not reset when i delete text fast in my searchbar android?为什么当我在我的搜索栏 android 中快速删除文本时我的 recyclerview 没有重置?
【发布时间】:2020-12-22 14:29:43
【问题描述】:

我的应用程序中有一个搜索功能,尽管由于 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;
}

@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{

             }
         }


     });

【问题讨论】:

  • “它停在那个特定的商店并且不刷新”是什么意思?
  • @AlexMamo 我搜索了让我们说“纽约咖啡”。它在(cardview)中显示了纽约咖啡项目。当我删除文本时,它不会删除它并返回我拥有的完整商店列表
  • 你有没有尝试在onTextChanged中移动该逻辑?
  • 是的,我做到了,但我会再试一次@AlexMamo
  • @AlexMamo 是的,还是一样

标签: java android firebase android-recyclerview google-cloud-firestore


【解决方案1】:

更改:

if(!s.toString().isEmpty()){
                searchInDB(s.toString().toLowerCase());
            }else{
               searchInDB("");
            }

到

if(!s.toString().isEmpty()){
                searchInDB(s.toString().toLowerCase());
            }else{
               recyclerView.setAdapter(null);
            }

【讨论】:

  • 它工作一瞬间,然后停留在我搜索的内容上。
猜你喜欢
  • 2021-11-30
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
  • 2021-04-22
  • 2017-01-01
  • 1970-01-01
相关资源
最近更新 更多