尝试在 recyclerview 中使用 searchview 时所有项目都消失了吗?

All itmes disappear when try use searchview in recyclerview?

大家 我有 recyclerView 从 firebase 数据库检索数据

我使用搜索视图和过滤器列表来搜索项目,但是当单击搜索视图搜索某些内容时,所有项目都消失了,搜索视图不起作用

我的主要 activity 是片段,因为我使用底部导航

public class RV_Worker_Fragment extends Fragment implements RecyclerView_InterFace {

private RecyclerView rv_worker;
private FirebaseDatabase db;
private ArrayList <WorkerModel> list;
private WorkerAdapter workerAdapter;
SearchView searchView;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.rv_persons_fragment,
            container, false);

    rv_worker =view.findViewById(R.id.recyclerView_persons);
    db =FirebaseDatabase.getInstance();
    rv_worker.setHasFixedSize(true);
    rv_worker.setLayoutManager(new LinearLayoutManager(getActivity()));

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

        @Override
        public boolean onQueryTextChange(String newText) {
            workerAdapter.getFilter().filter(newText);
            return false;
        }
    });

    getList();

    return view;
}

private void  getList()
{
    db.getReference("Employees").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            list=new ArrayList <>();
            workerAdapter = new WorkerAdapter(getActivity(),list,RV_Worker_Fragment.this);
            rv_worker.setAdapter(workerAdapter);
            for (DataSnapshot child :snapshot.getChildren())
            {
                WorkerModel workerModel =child.getValue(WorkerModel.class);
                list.add(workerModel);
                workerAdapter.notifyItemInserted(list.size()-1);
                workerAdapter.getItemCount();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public void onItemClick(final int position) {

    Intent intent = new Intent(getActivity(), Worker_Details.class);
    intent.putExtra("name",list.get(position).getName());
    intent.putExtra("id",list.get(position).getId());
    startActivity(intent);

}
}

适配器

public class WorkerAdapter extends RecyclerView.Adapter <WorkerAdapter.ViewHolder> implements Filterable {
private Context context;
private List <WorkerModel> list;
private List <WorkerModel> listFilter;
private RecyclerView_InterFace recyclerView_interFace;

public WorkerAdapter(Context context, List <WorkerModel> llist, RecyclerView_InterFace recyclerView_interFace) {
    this.context = context;
    this.list = llist;
    this.recyclerView_interFace = recyclerView_interFace;
    listFilter = new ArrayList <>(list);
}

@NonNull
@Override
public WorkerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.card_worker_treader_contractor, parent, false));
}

@Override
public void onBindViewHolder(@NonNull final WorkerAdapter.ViewHolder holder, final int position) {

    final WorkerModel workerModel = list.get(position);
    holder.tv_name.setText(workerModel.getName());
    holder.btn_call.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String phonNumber = workerModel.getPhone();
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:"+phonNumber));
            context.startActivity(intent);
        }
    });

    holder.btn_delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new AlertDialog.Builder(context)
                    .setTitle("حذف")
                    .setMessage("هل تريد الحذف")
                    .setPositiveButton("نعم", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            FirebaseDatabase database = FirebaseDatabase.getInstance();
                            DatabaseReference db = database.getReference("Employees");
                            db.child(list.get(position).getId()).removeValue();
                        }
                    })
                    .setNegativeButton("لا",null)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .show();
        }
    });
}

@Override
public int getItemCount() {
    return list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView tv_name;
    ImageView btn_call, btn_delete ;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        tv_name = itemView.findViewById(R.id.tx_name);
        btn_call = itemView.findViewById(R.id.btn_call);
        btn_delete = itemView.findViewById(R.id.btn_deleteP);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recyclerView_interFace.onItemClick(getAdapterPosition());
            }
        });
    }
}

@Override
public Filter getFilter() {
    return filter;
}

private Filter filter = new Filter() {

    //run on background thread
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {

        List<WorkerModel> listFilter2 = new ArrayList <>();
        if (charSequence ==null || charSequence.length()==0){
            // if search text is empty add all original list values to filterlist
            listFilter2.addAll(listFilter);
        }else {
            String filterPattern = charSequence.toString().toLowerCase().trim();
            // Perform search on whole original list
            for (WorkerModel workerModel : listFilter){
                if (workerModel.getName().toLowerCase().contains(filterPattern)){
                    listFilter2.add(workerModel);
                }
            }
        }
        FilterResults results = new FilterResults();
        results.values = listFilter2;
        return results;
    }

    //run on UI thread
    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
        list.clear();
        list.addAll((List) filterResults.values);
        notifyDataSetChanged();
    }
};

}

问题是什么? 提前感谢帮助

尝试在 WorkerAdapter 的 performFiltering() 中,添加以下行作为第一行:if (listFilter.size() == 0) listFilter = new ArrayList<>(list);