Activate CAB menu when OnClickEvent happens in Android

Trying to activate CAB menu when clicking on MenuItem from ActionBar. Here is how I set the GridView for listening to Multi Choice. The multiModeChoiceListener is working fine when I long press on Any item in the GridView. It is working fine. Now I have a requirement to activate the CAB menu when do press on a menu item in Action Bar. Once it is pressed, the CAB menu should read that 0 items are selected. After that it should allow me to select items from GridView on single clicks. How can I achieve this feature?

GridView set listener:

gv.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL); 
gv.setMultiChoiceModeListener(new MultiChoiceModeListener());

MultiChoiceModeListener.java

public class MultiChoiceModeListener implements
    GridView.MultiChoiceModeListener {  

public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    mode.getMenuInflater().inflate(R.menu.featured_multiselect, menu);
    MenuItem mi = menu.findItem(R.id.close);
    mi.setIcon(R.drawable.cancel);
    mode.setTitle("Select Items");
    return true;
}

public boolean onPrepareActionMode(ActionMode mode, Menu menu) {    
    return true;
}

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    Toast.makeText(getApplicationContext(), item.getTitle(),
            Toast.LENGTH_SHORT).show();
    if (item.getTitle().toString().equalsIgnoreCase("Close")) {
        mode.finish();
    }
    return true;
}

public void onDestroyActionMode(ActionMode mode) {
    new ChangeNotifier().changeOnFavoriteStore = true;
    new AddFavorites().execute("add", device_id, dataArray);
    if (notify == true) {
        Toast.makeText(getApplicationContext(),
                "Selected items are added to Favorites",
                Toast.LENGTH_SHORT).show();
        notify = false;
    }
}

public void onItemCheckedStateChanged(ActionMode mode, int position,
        long id, boolean checked) {
    int selectCount = gridView.getCheckedItemCount();
    if (selectCount > 0) {
        notify = true;              
        dataArray.add(position);
        switch (selectCount) {
        case 1:
            mode.setSubtitle("One item added to favorites");
            break;
        default:
            mode.setSubtitle("" + selectCount
                    + " items added to favorites");
            break;
        }
    } 

   }

OnMenuItemClick method:

 public boolean onPrepareOptionsMenu(final Menu menu) {

    final MenuItem editItem = menu.findItem(R.id.editit);

    editItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            //the CAB menu should be activated here. So that it reads that 0 items are selected in ActionBar

            return false;
        }

    });

Answers


From your question I understand that you're trying to start the GridView associated CAB from clicking one of the menu items. I don't know if you can do this(but I may be mistaken) as the MultiChoiceModeListener expects an item to be checked to start. Depending on your layout and the overall appearance of the GridView, I think you could have a dummy item(as an extra item in the adapter) at the end of the GridView(with no content showing) and use setItemChecked(dummyItemPosition, true) to start the GridView CAB. Of course you'll need to have additional logic to take care of that extra item in your MultiChoiceModeListener:

 public void onItemCheckedStateChanged(ActionMode mode, int position,
        long id, boolean checked) {
    if (position == theDummyPosition)
         return; // so we start the CAB but there aren't any items checked
    }
    int selectCount = gridView.getCheckedItemCount();
    if (selectCount > 0) {
        notify = true;              
        dataArray.add(position);
        // if you select another item you'll have two selected items(because of the dummy item) so you need to take care of it 
        switch (selectCount) {
        case 1:
            mode.setSubtitle("One item added to favorites");
            break;
        default:
            mode.setSubtitle("" + selectCount
                    + " items added to favorites");
            break;
        }
    } 

   }

The solution above is a hack, most likely it would be much easier to lose the MultiChoiceModeListener and simply start an ActionMode that you can manipulate for both situations.


Need Your Help

Ordering by the first alphabetical char in a column in MYSQL

database mysql

A table in a MYSQL database has address details- eg...

How to open a UDP port?

c++ network-programming udp port

I have an assignment that I need to write a program that opens a port in the network, than I should check if the port is opened in another machine using nmap or netcat.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.