Click here to Skip to main content
15,911,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to search two different strings from different columns in search view.For example instead of "city state",i want to search "ci sta" and get list of fields that contains the words. thank you

What I have tried:

public class ProviderRecyclerAdapter extends RecyclerView.Adapter<ProviderViewHolder> implements Filterable {

    private ArrayList<Provider_list> mDataset;
    private Context mContext;
    private ProviderFilter providerFilter;

    public ProviderRecyclerAdapter(ArrayList<Provider_list> dataset, Context context) {
        mDataset = dataset;
        mContext = context;
    }


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


    @Override
    public void onBindViewHolder(ProviderViewHolder holder, int position) {
        Provider_list n = mDataset.get(position);
        holder.mTitle.setText(n.getProvider_name());
        holder.mCity.setText(n.getCity());
        holder.mRegion.setText(n.getRegion());
        holder.mAddress.setText(n.getAddress());



    }

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


    @Override
    public Filter getFilter() {
        if(providerFilter == null)
            providerFilter = new ProviderFilter(this, mDataset);
        return providerFilter;
    }

    private static class ProviderFilter extends Filter {

        private final ProviderRecyclerAdapter brandsAdapter;
        private final List<Provider_list> originalList;
        private final List<Provider_list> filteredList;

        private ProviderFilter(ProviderRecyclerAdapter brandsAdapter, List<Provider_list> originalList) {
            super();
            this.brandsAdapter = brandsAdapter;
            this.originalList = new LinkedList<>(originalList);
            this.filteredList = new ArrayList<>();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            filteredList.clear();
            final FilterResults results = new FilterResults();

            if (constraint.length() == 0) {
                filteredList.addAll(originalList);
            } else {
                final String filterPattern = constraint.toString().toLowerCase().trim();

                for (final Provider_list business : originalList) {
                    if (business.getProvider_name().toLowerCase().contains(filterPattern)) {
                        filteredList.add(business);
                    }
                    else if (business.getAddress().toLowerCase().contains(filterPattern)){
                        filteredList.add(business);
                    }
                    else if (business.getRegion().toLowerCase().contains(filterPattern)){
                        filteredList.add(business);
                    }
                    else if (business.getCity().toLowerCase().contains(filterPattern)){
                        filteredList.add(business);
                    }
                    else if ( business.getCity().toLowerCase().contains(filterPattern) && business.getProvider_name().toLowerCase().contains(filterPattern) )
                    {
                        filteredList.add(business);
                    }


                }
            }
            results.values = filteredList;
            results.count = filteredList.size();
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            brandsAdapter.mDataset.clear();
            brandsAdapter.mDataset.addAll((List<Provider_list>) results.values);
            brandsAdapter.notifyDataSetChanged();
        }
    }
}
Posted
Comments
David Crow 13-Jul-17 12:53pm    
So what's the problem? Hint: "it doesn't work" is not the correct answer.
Member 13053943 14-Jul-17 6:39am    
did you read the question at all? i want to search for two incomplete different strings in the searchbar.when i type space and another word,the search doesn't work
David Crow 14-Jul-17 8:01am    
Yes, I read the question. However, you did not mention what the problem was. In any case, you'll need to modify performFiltering(). Take the constraint and break it up into substrings (delimited by space). For each of the substrings found, search the appropriate fields in Provider_list.
Member 13053943 14-Jul-17 8:25am    
there is no problem with the code.i am finding it difficult to do that type of search i want.like looking for "ci st",i should have list of data containing that.my problem is the space between the strings which i can't add to the code.if you can write down some codes for me i'll appreciate it.thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900