I want to search through RecyclerView
, I have List<BaseOfCards>
(BaseOfCards is my getter&setter class)
My RecyclerViewAdapter :
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private LayoutInflater inflater;
private List<BaseOfCards> items;
//private int itemLayout;
//String cardvalue;
private Activity mActivity;
public RecyclerViewAdapter(Activity mActivity, Context context, List<BaseOfCards> items) {
this.mActivity = mActivity;
inflater = LayoutInflater.from(context);
this.items = items;
//this.itemLayout = itemLayout;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_row, parent, false);
MyViewHolder holder = new MyViewHolder(view, mActivity);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
BaseOfCards item = items.get(position);
holder.title.setTag(item);
holder.title.setText(item.getCardName());
}
@Override
public int getItemCount() {
return items.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private Activity mActivity;
TextView title;
ImageView titileImageView;
public MyViewHolder(View itemView, Activity mActivity) {
super(itemView);
titileImageView = (ImageView) itemView.findViewById(R.id.image_country);
title = (TextView) itemView.findViewById(R.id.listText);
this.mActivity = mActivity;
}
}
}
I add SearchView to my menu and initialize it in MainActivity:
MenuItem menuItem = menu.findItem(R.id.action_search1);
searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setIconifiedByDefault(true);
What do I need to do next? Make my RecyclerViewAdapter implement Filterable
or what? Or just make class filter
in *RecyclerViewAdapter** and just call it from my MainActivity ?
I solved my problem
Make my class RecyclerViewAdapter implements Filterable
Add line private List<BaseOfCards> orig;
Add method getFilter
in RecyclerViewAdapter
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final List<BaseOfCards> results = new ArrayList<BaseOfCards>();
if (orig == null)
orig = items;
if (constraint != null){
if(orig !=null & orig.size()>0 ){
for ( final BaseOfCards g :orig) {
if (g.getCardName().toLowerCase().contains(constraint.toString()))results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
items = (ArrayList<BaseOfCards>)results.values;
notifyDataSetChanged();
}
};
Make MainActivity implements SearchView.OnQueryTextListener
and change method onQueryTextChange
:
@Override
public boolean onQueryTextChange(String newText) {
if ( TextUtils.isEmpty ( newText ) ) {
adapter.getFilter().filter("");
} else {
adapter.getFilter().filter(newText.toString());
}
return true;
}
In your adapter class extend filterable. Then override the public Filter getFilter().
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults filterResults = new FilterResults();
if(charSequence == null | charSequence.length() == 0){
filterResults.count = getUserModelListFiltered.size();
filterResults.values = getUserModelListFiltered;
}else{
String searchChr = charSequence.toString().toLowerCase();
List<UserModel> resultData = new ArrayList<>();
for(UserModel userModel: getUserModelListFiltered){
if(userModel.getUserName().toLowerCase().contains(searchChr)){
resultData.add(userModel);
}
}
filterResults.count = resultData.size();
filterResults.values = resultData;
}
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
userModelList = (List<UserModel>) filterResults.values;
notifyDataSetChanged();
}
};
return filter;
}
in your main activity add search view and listener to onQueryTextChange.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem menuItem = menu.findItem(R.id.search_view);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
usersAdapter.getFilter().filter(newText);
return true;
}
});
return true;
}
Full tutorial and source code. Recyclerview with SearchView
I want to add to ololoking answer.
In MainActivity
we should also add next code so it would work:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setIconifiedByDefault(true);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
Thanks ololoking for your answer. It helped me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With