I have implemented the search functionality in my android application along with the sorting of recycler view items in ascending and descending order.I have used action bar icons to perform these operations.My ascending and descending order icons works properly when I don't click search bar icon at all,but as soon as I click on my search bar icon to search something and then I try to sort my recycler view list by clicking on the sorting icons then it doesnt performs sorting.What is the issue? Here is my code for my activity and adapter-->
food_item_description.java
public class food_item_description extends AppCompatActivity implements SearchView.OnQueryTextListener{ private food_description_adapter adapter; private RecyclerView recyclerView; databasehandler db; private String Food_category; private List<food_items> food_itemses; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.food_description); db=databasehandler.getInstance(getApplicationContext()); recyclerView=(RecyclerView)findViewById(R.id.recycler_view2); Food_category=getIntent().getStringExtra("category_food"); setTitle(Food_category); food_itemses = new ArrayList<>(); food_itemses.addAll(db.getAllfood_items(Food_category)); adapter=new food_description_adapter(food_itemses); recyclerView.addItemDecoration(new recycler_view_divider(this, LinearLayoutManager.VERTICAL)); recyclerView.setHasFixedSize(true); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.food_items, menu); final MenuItem searchItem = menu.findItem(R.id.menu_search); final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setOnQueryTextListener(this); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. // ActionBarDrawerToggle will take care of this. switch (item.getItemId()) { case R.id.ascending: Collections.sort(food_itemses, new Comparator<food_items>() { @Override public int compare(food_items lhs, food_items rhs) { return (int) (lhs.calorie-rhs.calorie); } }); adapter.notifyDataSetChanged(); break; case R.id.descending: Collections.sort(food_itemses, new Comparator<food_items>() { @Override public int compare(food_items lhs, food_items rhs) { return (int) (rhs.calorie-lhs.calorie); } }); adapter.notifyDataSetChanged(); break; case R.id.menu_search: MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() { @Override public boolean onMenuItemActionCollapse(MenuItem item) { // Do something when collapsed adapter.setFilter(food_itemses); return true; // Return true to collapse action view } @Override public boolean onMenuItemActionExpand(MenuItem item) { // Do something when expanded return true; // Return true to expand action view } }); break; default: break; } return super.onOptionsItemSelected(item); } @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { final List<food_items> filteredModelList = filter(food_itemses, newText); adapter.setFilter(filteredModelList); return true; } private List<food_items> filter(List<food_items> models, String query) { query = query.toLowerCase(); final List<food_items> filteredModelList = new ArrayList<>(); for (food_items model : models) { final String text = model.getfood_item().toLowerCase(); if (text.contains(query)) { filteredModelList.add(model); } } return filteredModelList; } }
food_description_adapter.java
public class food_description_adapter extends RecyclerView.Adapter<com.samarth.lightweight.database.food_description_adapter.ViewHolder> { private List<food_items> food_items; private List<food_items> original_food_items; public food_description_adapter(List<food_items> food_itemses) { food_items=food_itemses; original_food_items=food_itemses; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.food_description_layout, parent, false); return new ViewHolder(itemView); } public class ViewHolder extends RecyclerView.ViewHolder { public TextView food_item; public TextView prot_value; public TextView fat_value; public TextView carb_value; public TextView cal_value; public ImageView bk,bk1; public ViewHolder(View view) { super(view); food_item = (TextView)view.findViewById(R.id.food_item); prot_value= (TextView)view.findViewById(R.id.prot_value); fat_value= (TextView)view.findViewById(R.id.fat_value); carb_value= (TextView)view.findViewById(R.id.carb_value); cal_value= (TextView)view.findViewById(R.id.cal_value); bk=(ImageView) view.findViewById(R.id.bk); bk1=(ImageView) view.findViewById(R.id.bk1); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)food_item.getLayoutParams(); params.addRule(RelativeLayout.LEFT_OF, R.id.bk1); food_item.setLayoutParams(params); //causes layout update } } @Override public void onBindViewHolder(final com.samarth.lightweight.database.food_description_adapter.ViewHolder viewHolder, int position) { com.samarth.lightweight.database.food_items food_item=food_items.get(position); viewHolder.food_item.setText(food_item.getfood_item()); viewHolder.prot_value.setText(String.valueOf(food_item.getproteins())+ "g."); viewHolder.fat_value.setText(String.valueOf(food_item.getfat())+ "g."); viewHolder.carb_value.setText(String.valueOf(food_item.getcarb())+ "g."); viewHolder.cal_value.setText(String.valueOf(food_item.getcalorie())); viewHolder.bk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewHolder.bk.setVisibility(View.GONE); viewHolder.bk1.setVisibility(View.VISIBLE); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)viewHolder.food_item.getLayoutParams(); params.addRule(RelativeLayout.LEFT_OF, R.id.bk1); viewHolder.food_item.setLayoutParams(params); //causes layout update } }); viewHolder.bk1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewHolder.bk1.setVisibility(View.GONE); viewHolder.bk.setVisibility(View.VISIBLE); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)viewHolder.food_item.getLayoutParams(); params.addRule(RelativeLayout.LEFT_OF, R.id.bk); viewHolder.food_item.setLayoutParams(params); //causes layout update } }); } @Override public int getItemCount() { if(food_items!=null) return food_items.size(); return 0; } public void setFilter(List<food_items> foods){ food_items = new ArrayList<>(); food_items.addAll(foods); notifyDataSetChanged(); } }
food_items.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/ascending" android:orderInCategory="100" app:showAsAction="always" android:icon="@drawable/ic_arrow_drop_up_white_24dp" android:title="Ascending"/> <item android:id="@+id/descending" android:orderInCategory="100" app:showAsAction="always" android:icon="@drawable/ic_arrow_drop_down_white_24dp" android:title="Descending"/> <item android:id="@+id/menu_search" android:title="Search" android:icon="@drawable/ic_search_white_24dp" app:showAsAction="always|collapseActionView" app:actionViewClass="android.support.v7.widget.SearchView" /> </menu>