I Want To Remove The Optionmenu When There Are No Items In Listview (emptyview Is Shown)?
Solution 1:
the correct condition to put inside onPrepareOptionsMenu
is:
menuItem.setVisible(!mAdapter.isEmpty());
that is the same comparison the ListView uses manages the empty view (minus null check) (https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/AdapterView.java#747)
but I believe there's another issue here, is that the data is changing after the activity started, during showDeleteConfirmationDialog()
. That means you have to call invalidateOptionsMenu()
the moment the data is changed. There're two ways of doing it. One more robust and the other is faster to code:
- faster to code (but not very good/clean):
add invalidateOptionsMenu()
after the code that executes the DB operations.
- more robust/clean
you'll use start/stop callbacks to listen to changes in the data. Something like the following:
@OverrideprotectedvoidonStart(){
super.onStart();
invalidateOptionsMenu();
mAdapter.registerDataSetObserver(dataObserver);
}
@OverrideprotectedvoidonStop(){
mAdapter.unregisterDataSetObserver(dataObserver);
super.onStop();
}
private final DataSetObserver dataObserver = newDataSetObserver(){
@OverridepublicvoidonChanged(){
invalidateOptionsMenu();
}
@OverridepublicvoidonInvalidated(){
invalidateOptionsMenu();
}
};
all code above was typed by heart, there're likely typos, but that's the idea and the typos you can fix later at your code.
Solution 2:
if you dont have any menu item to show, why are u inflating one, if you dont inflate you wont.
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
returnfalse;
}
Unless you need to dynamically remove the menu options based on the current state of the activity, this might help How do you remove an inflated menu/items from the new Lollipop Toolbar?
Post a Comment for "I Want To Remove The Optionmenu When There Are No Items In Listview (emptyview Is Shown)?"