我正在开发一个显示不同电影的应用程序,我的菜单上有三个选项:评分最高,最受欢迎和最喜欢的。
我通过这种方式控制通过布尔显示的电影:
到目前为止,当我在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);
}
提前致谢:)
答案 0 :(得分:0)
我成功解决了我的问题!以下是我的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);
}
答案 1 :(得分:0)
要使适配器获取新的数据集,您需要在传入新数据之前清除旧数据,然后在完成后通知适配器。您可以使用此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;