简体   繁体   English

android - 在onOptionsItemSelected上更改布尔值

[英]android - Change boolean value on onOptionsItemSelected

I am developping an app displaying different movies, with three options on my menu : top rated, most popular and favorites. 我正在开发一个显示不同电影的应用程序,在我的菜单上有三个选项:评分最高,最受欢迎和最喜欢的。

I control the movies displayed through boolean, in this way : 我通过这种方式控制通过布尔显示的电影:

  • isFavorite = true && is topRated = false => display the favorites isFavorite = true &&是topRated = false =>显示收藏夹
  • isFavorite = false && topRated = true => display the topRated isFavorite = false && topRated = true =>显示topRated
  • isFavorite = false && topRated = false => display the most popular isFavorite = false && topRated = false =>显示最受欢迎

So far, when I set the value manually on the OnCreate(), it works ! 到目前为止,当我在OnCreate()上手动设置值时,它可以工作!

My issue comes when I try to change this value by clicking on the differents options of the menu... it displays the same list of movies independently of the option I click on. 当我尝试通过单击菜单的不同选项来更改此值时,我的问题出现了...它显示的是与我单击的选项无关的相同电影列表。

Here is my onOptionItemSelected() method : 这是我的onOptionItemSelected()方法:

public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();


    switch (itemId){
        case R.id.most_popular:
            setFavorite(false);
            setTopRated(false);
            movieAdapter.notifyDataSetChanged();


            Context context = MainActivity.this;
            String textToShow = "Sorted by most popular";
            Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();
            break;

        case R.id.highest_rated:
            setFavorite(false);
            setTopRated(true);

            movieAdapter.notifyDataSetChanged();
            context = MainActivity.this;
            textToShow = "Sorted by rate";
            Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();
            break;

        case R.id.favorites:
            setFavorite(true);
            setTopRated(false);

            movieAdapter.notifyDataSetChanged();
            context = MainActivity.this;
            textToShow = "Here is your favorites list";
            Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();
            break;

        default:
            Log.w(TAG, "Menu selection is not handled. ItemId;" + itemId);

    }
    return super.onOptionsItemSelected(item);
}

Thanks in advance :) 提前致谢 :)

I succeed to resolve my issue ! 我成功解决了我的问题! Here are the modifications, on my MakeTheQuery() method and onOptionItemSelected ; 以下是我的MakeTheQuery()方法和onOptionItemSelected的修改;

private void makeTheQuery() {
    movies.clear();
    isFavorite = isFavorite();
    isTopRated = isTopRated();

    URL SearchUrl;
    if (!isTopRated) {
        SearchUrl = NetworkUtils.buildUrl();
    }else{
        SearchUrl = NetworkUtils.buildUrlTopRated();
    }
    new TheMovieAsyncTask().execute(SearchUrl);
}

and

public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();


    switch (itemId){
        case R.id.most_popular:
            setFavorite(false);
            setTopRated(false);
            movieAdapter.notifyDataSetChanged();
            makeTheQuery();

            Context context = MainActivity.this;
            String textToShow = "Sorted by most popular";
            Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();
            break;

        case R.id.highest_rated:
            setFavorite(false);
            setTopRated(true);

            movieAdapter.notifyDataSetChanged();
            makeTheQuery();


            context = MainActivity.this;
            textToShow = "Sorted by rate";
            Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();
            break;

        case R.id.favorites:
            setFavorite(true);
            setTopRated(false);

            movieAdapter.notifyDataSetChanged();

            makeTheQuery();

            context = MainActivity.this;
            textToShow = "Here is your favorites list";
            Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();
            break;

        default:
            Log.w(TAG, "Menu selection is not handled. ItemId;" + itemId);

    }
    return super.onOptionsItemSelected(item);
}

For the adapter to get new set of data, you need to clear the the old data before passing in the new ones and then notify the adapter when you're done. 要使适配器获取新的数据集,您需要在传递新数据之前清除旧数据,然后在完成后通知适配器。 You can do that with this movieAdapter.clear(); 你可以用这个movieAdapter.clear(); ... Here's how to implement it: ......以下是如何实现它:

    case R.id.highest_rated:
//Clear the adapter whenever this highest rated movie is clicked
                movieAdapter.clear(); //Clear the adapter whenever this highest rated movie is clicked
                setFavorite(false);
                setTopRated(true);

                movieAdapter.notifyDataSetChanged();
                context = MainActivity.this;
                textToShow = "Sorted by rate";
                Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();
                break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM