Implementing Multi-Selection in Android ListView

This is a feature I've wanted to implement for a while but wasn't sure how. I finally figured it out these past few days, so I'm writing it down here to record it.

To implement multi-selection for a ListView, you need to set a ChoiceMode and a multi-selection listener. ChoiceMode refers to how many selections the user can make on the ListView (single, multiple). The ChoiceMode for multi-selection is set like this:

lv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);

Note: To use AbsListView, you need to import android.widget.AbsListView beforehand.

While changing the ListView, it's also best to change the layout in your array adapter to this, so the user knows what they've selected:

android.R.layout.simple_list_item_multiple_choice

Implementing the multi-selection listener is also very simple:


rideListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
    @Override
    public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
        // Called when an item's checked state has changed
        // Update the title or count of selected items here if needed
    }

    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        // Called when the action mode is created.
        // Inflate the menu for the action mode here.
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.your_multi_select_menu, menu); // Replace your_multi_select_menu with your actual menu file name
        return true; // Return true to indicate the action mode should be created
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
        // Called each time the action mode is shown. Always called after onCreateActionMode.
        // Use if you need to dynamically change menu items.
        return false; // Return false if nothing is done
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
        // Called when a menu item is clicked.
        switch (menuItem.getItemId()) {
            // Handle menu item clicks here
            // case R.id.menu_delete:
            //     // Perform deletion
            //     actionMode.finish(); // Close the action mode
            //     return true;
            // case R.id.menu_share:
            //     // Perform sharing
            //     actionMode.finish();
            //     return true;
            default:
                return false; // Return false if the menu item click was not handled
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode) {
        // Called when the action mode is about to be exited.
        // Clean up selections or other state changes here.
    }
});

The IDE will automatically bring in the methods above. You need to pay attention to onCreateActionMode and onActionItemClicked.

Inside the onCreateActionMode method, you inflate a menu that appears after multi-selection. This is necessary; otherwise, no action menu will appear after selecting multiple items. R.menu.your_multi_select_menu here refers to the menu file name located in the menu folder (please replace your_multi_select_menu with your actual file name).

The onActionItemClicked method detects the operation when an item in the menu is clicked.

With these steps, your ListView will be capable of multi-selection.

Comments

Popular posts from this blog

我係點樣喺 ozark 丟咗我部電話

圣路易斯餐厅指南

How to use GPS locations in Android