Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
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 = false && topRated = true => display the topRated
  • isFavorite = false && topRated = false => display the most popular

So far, when I set the value manually on the OnCreate(), it works !

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 :

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 :)

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I succeed to resolve my issue ! Here are the modifications, on my MakeTheQuery() method and 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);
}
over 4 years ago · Santiago Trujillo Report

0

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();... 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;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!