1

I have a ListView in my activity. I also have a menu and there is a menuitem for saving the list. I want to make it behave like the pseudocode below:

if (myList.size() > 0) {
    menuitem.setVisible(true);
} else {
    menuitem.setVisible(false);
}

I want the "save" menuitem to show only when there is at least one item in the listview. I have a custom ArrayAdapter implementation also, can this action be made from there? Or is there any kind of listener I could use?

Any help is appreciated!

EDIT:

JDev answer is the solution. I changed only one thing: I moved the list size checking to notifyDataSetChanged() method inside my custom ArrayAdapter and now it works!

     /**
     * {@inheritDoc}
     */
    @Override
    public void notifyDataSetChanged() {

        // Hide/show 'save' menuitem
        if (mListItems.size() > 0) {
           mListener.setMenuItemVisible(true);
        } else {
           mListener.setMenuItemVisible(false);
        }

        super.notifyDataSetChanged();
     }
lkallas
  • 1,310
  • 5
  • 22
  • 36
  • http://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar, http://stackoverflow.com/questions/12894802/how-to-remove-menuitems-from-menu-programmatically. doe's it's help? – AsfK Nov 19 '15 at 06:50
  • @AsfK Not really. I know how to hide a menuitem, but I don't know how to hide it when there is no items in my ListView and how to show it when at least one item has been put to the ListView. – lkallas Nov 19 '15 at 06:56
  • 2
    If you're using custom listview you should found `getCount()` method, otherwise you can know your list\array length(\size). just use your code when you add\remove an item from the listview. Show some code, where you add\remove items... – AsfK Nov 19 '15 at 07:02
  • @lkallas did you solve your problem ? – Mohammed Aouf Zouag Nov 19 '15 at 07:34
  • 1
    @JDev Yeah, it works nicely! I Edited my question. Could you tell me if super.notifyDataSetChanged(); is needed in notifyDataSetChanged() or not? Anyways upvoted and marked as answer. Thank you very much! – lkallas Nov 19 '15 at 08:01
  • @lkallas I really don't know, because I've never needed to override the method. Anyway your problem is solved, good for you ! – Mohammed Aouf Zouag Nov 19 '15 at 14:54

3 Answers3

3

After getting the ListView length. Add invalidateOptionsMenu(); to call onCreateOptionsMenu().

Check if ListView is empty or not here,

@Override 
public boolean onCreateOptionsMenu(Menu menu)
{ 
    ...
    MenuItem menuitem = menu.findItem(R.id.addAction);
    if (myList.size() > 0) {
        menuitem.setVisible(true);
    } else {
        menuitem.setVisible(false);
    }
}

Comment below if you have any query.

activesince93
  • 1,716
  • 17
  • 37
1

In your custom array adapter, you can do something like this:

CustomArrayAdapter.java

public interface MenuListener {
    void setMenuItemVisible(boolean state);
}

private MenuListener mListener;
private Item[] mListItems; // The list of items that your adapter handles

public void setMenuListener(MenuListener listener) {
    mListener = listener;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ListView listview = convertView.findViewById(R.id.yourListViewId);
    if (mListItems.size() > 0) {
        mListener.setMenuItemVisible(true);
    } else {
        mListener.setMenuItemVisible(false);
    }

}

Activity.java:

public class Activity implements CustomArrayAdapter.MenuListener {

    private MenuItem menuItem; // The menu item that you wanna show/hide

    ....

    @Override
    public void onCreate(Bundle savedInstanceState) {

        CustomArrayAdapter adapter = new CustomArrayAdapter(params...);
        adapter.setMenuListener(this);
        // Set listView adapter
    }

    @Override 
    public boolean onCreateOptionsMenu(Menu menu)
    { 
        menuItem = menu.findItem(R.id.menuItemId); // Save the menu item that you want to show/hide later
        ...
    }

    @Override
    public void setMenuItemVisible(boolean state) {
        menuItem.setVisible(state);
    }
}
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
1

You shouldn't have to implement listeners or other convoluted methods for something as simple as this. In your Activity, simply add one line (after you've fetched your array to be passed on to your adapter):

menuitem.setVisible(array!= null && array.length() > 0 ? true: false);
// array.length() / array.length / array.size() -- whichever is suitable

EDIT: if you're allowing users to add/remove from the list, then disregard the one-liner above. You could simply include your Activity as a parameter to your adapter's constructor. In your activity, add a public method such as:

public void showHideMenuItem(int size) {
    menuitem.setVisible(size > 0 ? true: false);
}

In your adapter, call the above method from wherever you're allowing adding/removing of items:

activity.showHideMenuItem(items.getCount());

To set the menuitem's visibility initially, call:

showHideMenuItem(0); // from your Activity's onCreate(), or
activity.showHideMenuItem(0); // from your adapter's constructor

If you've solved this via listener etc and you're happy with it, that's ok, I just personally wouldn't complicate stuff if it isn't necessary. I'd also prefer to determine the menuitem's visibility via the size of the array held by the adapter vs. the number of items in the listview.

mjp66
  • 4,214
  • 6
  • 26
  • 31
  • But where do I put this line in my Activity java code? I need the menuitem to bee invisible when listview is empty and as soon as there is something it becomes visible again. It seems logical to perform this action when ListView's dataset has changed. Or not? – lkallas Nov 19 '15 at 08:27
  • Will users be able to add/remove items from the listview? – mjp66 Nov 19 '15 at 08:35
  • Yes! Users are able to add/remove. – lkallas Nov 19 '15 at 08:44