搜索视图栏和其他操作栏图标无法正常工作

时间:2017-02-16 06:57:17

标签: java android sorting

我已经在我的Android应用程序中实现了搜索功能以及按升序和降序排列回收站视图项目。我使用了操作栏图标来执行这些操作。当我不知道时,升序和降序图标正常工作t点击搜索栏图标,但是一旦我点击我的搜索栏图标搜索某些内容然后我尝试通过点击排序图标对我的回收站视图列表进行排序,然后它就不会执行排序。问题是什么? 这是我的活动代码和适配器 - >

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>

action bar display

1 个答案:

答案 0 :(得分:0)

我通过在MenuItemCompat.setOnActionExpandListener中应用onCreateOptionsMenu并因此添加以下代码来解决了这个问题 -

MenuItemCompat.setOnActionExpandListener(searchItem,

                        new MenuItemCompat.OnActionExpandListener() {
                            @Override
                            public boolean onMenuItemActionCollapse(MenuItem item) {
                                // Do something when collapsed
                                Log.i("search bar","collapsed");
                                adapter.setFilter(food_itemses);
                                return true; // Return true to collapse action view
                            }

                            @Override
                            public boolean onMenuItemActionExpand(MenuItem item) {
                                // Do something when expanded
                                Log.i("search bar","expanded");
                                return true; // Return true to expand action view
                            }
                        });

我也做了一些其他的改动。所以这是我最后改进的代码 -

<强> 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;
    private List<food_items> filtered_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<>();
        filtered_food_itemses=new ArrayList<>();

        food_itemses.addAll(db.getAllfood_items(Food_category));
        filtered_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);
        MenuItemCompat.setOnActionExpandListener(searchItem,

                        new MenuItemCompat.OnActionExpandListener() {
                            @Override
                            public boolean onMenuItemActionCollapse(MenuItem item) {
                                // Do something when collapsed
                                Log.i("search bar","collapsed");
                                adapter.setFilter(food_itemses);
                                return true; // Return true to collapse action view
                            }

                            @Override
                            public boolean onMenuItemActionExpand(MenuItem item) {
                                // Do something when expanded
                                Log.i("search bar","expanded");
                                return true; // Return true to expand action view
                            }
                        });
        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(filtered_food_itemses, new Comparator<food_items>() {
                    @Override
                    public int compare(food_items lhs, food_items rhs) {
                        return (int) (lhs.calorie-rhs.calorie);
                    }
                });
                adapter.setFilter(filtered_food_itemses);
                adapter.notifyDataSetChanged();
                break;
            case R.id.descending:
                Collections.sort(filtered_food_itemses, new Comparator<food_items>() {
                    @Override
                    public int compare(food_items lhs, food_items rhs) {
                        return (int) (rhs.calorie-lhs.calorie);
                    }
                });
                adapter.setFilter(filtered_food_itemses);
                adapter.notifyDataSetChanged();
                break;

            default:
                break;

        }
        return super.onOptionsItemSelected(item);
}

    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);
            }
        }
        filtered_food_itemses=filteredModelList;
        return filteredModelList;
    }


    @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;
    }
}