【问题标题】:android - Change boolean value on onOptionsItemSelectedandroid - 更改 onOptionsItemSelected 上的布尔值
【发布时间】:2017-04-28 12:15:50
【问题描述】:

我正在开发一个显示不同电影的应用程序,我的菜单上有三个选项:评分最高、最受欢迎和最喜欢的。

我通过布尔值控制显示的电影,这样:

  • isFavorite = true && is topRated = false => 显示收藏夹
  • isFavorite = false && topRated = true => 显示 topRated
  • isFavorite = false && topRated = false => 显示最受欢迎的

到目前为止,当我在 OnCreate() 上手动设置值时,它可以工作!

当我尝试通过单击菜单的不同选项来更改此值时,我的问题出现了......它显示相同的电影列表,独立于我单击的选项。

这是我的 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);
}

提前致谢:)

【问题讨论】:

  • 请不要依赖外部链接,在帖子中添加您的代码,我们会尽力找到答案
  • onCreate 只加载收藏的电影。你在哪里实现过滤逻辑?
  • @M.Situation,谢谢,我是新来的,我没想到。我刚刚编辑了我的消息。 @W
  • @WalterPalladino,我在我的 AynckTask 上实现了它

标签: java android


【解决方案1】:

为了让适配器获取新的数据集,您需要在传入新数据之前清除旧数据,然后在完成后通知适配器。你可以用这个movieAdapter.clear(); 来做到这一点......这是实现它的方法:

    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;

【讨论】:

  • 谢谢!我还没有看到你的答案,但我只是想出了同样的事情:)
【解决方案2】:

我成功解决了我的问题!这是我的 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);
}

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

【讨论】:

    猜你喜欢
    • 2014-01-06
    • 1970-01-01
    • 2012-10-29
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2012-12-30
    • 1970-01-01
    相关资源
    最近更新 更多