Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
Android: cambia el valor booleano en onOptionsItemSelected

Estoy desarrollando una aplicación que muestra diferentes películas, con tres opciones en mi menú: mejor calificadas, más populares y favoritas.

Controlo las películas que se muestran a través de booleanos, de esta manera:

  • isFavorite = true && is topRated = false => mostrar los favoritos
  • isFavorite = false && topRated = true => muestra la mejor calificación
  • isFavorite = false && topRated = false => mostrar los más populares

Hasta ahora, cuando establecí el valor manualmente en OnCreate(), ¡funciona!

Mi problema surge cuando trato de cambiar este valor haciendo clic en las diferentes opciones del menú... muestra la misma lista de películas independientemente de la opción en la que haga clic.

Aquí está mi método 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); }

Gracias por adelantado :)

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

¡Logre resolver mi problema! Aquí están las modificaciones, en mi método MakeTheQuery() y 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); }

y

 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 Relatório

0

Para que el adaptador obtenga un nuevo conjunto de datos, debe borrar los datos antiguos antes de pasar los nuevos y luego notificar al adaptador cuando haya terminado. Puedes hacer eso con este movieAdapter.clear(); ... He aquí cómo implementarlo:

 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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda