Home » Android » java – Implementing searchview with recyclerview-Exceptionshub

java – Implementing searchview with recyclerview-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I am trying to implement a searchview that will pullback all students that match the text entered. At the moment it currently does this but when the searchview text is removed or another value added it shows nothing as the list is empty and im not sure where it is going wrong. I have tried using adapter.updateData(list) but still nothing.

Fragment

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        // Do something that differs the Activity's menu here
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView)searchItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                System.out.println("before" + list);
                adapter.getFilter().filter(newText);
                System.out.println("after" + list);
                return false;
            }

        });

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView= inflater.inflate(R.layout.fragment_show_sightings_open,container,false);
        recycle=(RecyclerView)mView.findViewById(R.id.recycler);
        recycle.setHasFixedSize(true);
        recycle.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new RecyclerViewAdapter(getActivity(), list);
        recycle.setAdapter(adapter);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Names");

        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    New studentDetails = dataSnapshot.getValue(New.class);
                    Map<String, String> map = (Map) dataSnapshot.getValue();
                    String message = map.get("name");
                    if(message.equals("name"))
                    {
                        list.add(studentDetails);
                    }

                }

                //tried using adapter.updateData(list) not working either
                adapter.notifyDataSetChanged();


            }

Adapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements Filterable {

    Context context;
    ArrayList<New> arrayList;
    ArrayList<New> arrayListFiltered;

    public RecyclerViewAdapter(Context context, ArrayList<New> arrayList ) {
        this.arrayList  = arrayList ;
        this.arrayListFiltered = new ArrayList<>(arrayList);
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        New studentDetails = arrayList.get(position);
        holder.setTitle(studentDetails.getType().toString());

    }
    @Override
    public int getItemCount() {

        return arrayList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

    private Filter exampleFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            List<New> filteredList = new ArrayList<>();

            if(constraint == null || constraint.length()==0)
            {
                filteredList.addAll(arrayListFiltered);
            }
            else
            {
                String filterPattern = constraint.toString().toLowerCase().trim();

                for(New item: arrayListFiltered)
                {
                    if(item.getType().toString().toLowerCase().contains(filterPattern))
                    {
                        filteredList.add(item);
                    }
                }
            }
            FilterResults results= new FilterResults();
            results.values = filteredList;

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            arrayList.clear();
            arrayList.addAll((List)results.values);
            notifyDataSetChanged();

        }
    };


    public void updateData(ArrayList<New> myData){
        arrayListFiltered = myData;
        notifyDataSetChanged();
    }
How to&Answers: