0

I have implemented a custom contextual action bar with two buttons : one for deleting selected items from a listview and the other one for editing selected item . What I am trying to do is to make the editButton invisible when two or more items have been selected. I tried doing it this way but nothing happens:

public void OnItemCheckedStateChanged (ActionMode mode, int position, long id, bool check)
{
    SetSubtitle (mode);
    if (listview.CheckedItemCount > 1) {
        disableButtonFlag = true;
    } else
        disableButtonFlag = false;

    self.InvalidateOptionsMenu();
}

public bool OnCreateActionMode (ActionMode mode, IMenu menu)
{
    self.MenuInflater.Inflate (Resource.Menu.CAB_menu, menu);
    if (disableButtonFlag) {
        menu.FindItem(Resource.Id.action_edit).SetVisible(false);
    } else {
        menu.FindItem(Resource.Id.action_edit).SetVisible(true);            
    }
    mode.Title = "Select Items";
    SetSubtitle (mode);
    return true;
}
Stefan Stefanov
  • 829
  • 7
  • 23
  • Have you single-stepped your code to check if your rows "...SetVisible(..)" are getting executed? – C4d Oct 23 '14 at 13:54
  • I don't think OnCreationActionMode() is being called at all .. – Stefan Stefanov Oct 23 '14 at 14:15
  • Stop guessing, proof it. Would take about 1-2 minutes to check that. Set a breakpoint on the first line in OnItemCheckedStateChanged() and debug your code. Single-step through it and check which way it is running through. Where OnCreateActionMode() isnt called anywhere I guess it should act as an Event. So, is the handler for this event set? – C4d Oct 24 '14 at 07:07
  • Okay I did what you told me and yeah these rows are being called but only when the action bar is being created, when the self.InvalidateOptionsMenu(); is being called the OnCreationActionMode() is not being called at all .. But if the action bar has been closed and then recreated again manually then the button is set to invisible or visible depending on the number of items that have been selected previous time .. – Stefan Stefanov Oct 27 '14 at 08:25
  • See, there you go. No there is a little bit more information about where the problem is. Maybe you picked the wrong event for what you are trying to do. Ill check the out. – C4d Oct 27 '14 at 08:32

2 Answers2

0

This is how handling multiple items works for me:

private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(listView.SelectedIndices.Count > 1)
            {
                MessageBox.Show("Multiple rows selected!");
            }
        }  

If selected index changes, check for how many indices are checked. If more than 1 (=multiple), fire your code.

C4d
  • 3,183
  • 4
  • 29
  • 50
0

Finally I found my mistake! It was that instead of declaring:

  if (listview.CheckedItemCount > 1) {
        disableButtonFlag = true;
    } else
        disableButtonFlag = false;

within my OnCreateActionMode method and calling Activity.InvalidateOptionsMenu() in my OnItemCheckedStateChanged()method I should have declared these rows within my OnPrepareActionMode() method and then called ActionMode.Invalidate() within the OnItemCheckedStateChanged()method.

Stefan Stefanov
  • 829
  • 7
  • 23