Android CAB destroying after listview notifydatasetchanged

403 Views Asked by At

I am using setMultiChoiceModeListener on Listview to invoke Action Mode for selecting multiple items to be deleted. On ListView scroll I load more data from server and notify the adapter. After adapter has been notified, the Action Mode (if invoked) is destroyed and recreated, which makes the title and selected arraylist empty (You can see the picture below). While the listview item selection remains. I want the CAB to be persistent like we see it in Gmail app, where it does not destroy on loading more data.

enter image description here

Below is the code for Action Mode

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
            listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
                @Override
                public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

                        Item mItem = adapter.items.get(position);
                        if (mItem.isSection()) {
                        } else {
                            mode.setTitle(listView.getCheckedItemCount() + " Selected");
                            EntryItem mEntryItem = (EntryItem) mItem;
                            orderid = mEntryItem.orderId;
                            if (checked) {
                                selectedIdList.add(orderid);
                            } else {
                                selectedIdList.remove(orderid);
                            }
                            // Toggle the state of item after every click on it
                            adapter.toggleSelection(position);
                    }
                }

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    if (item.getItemId() == R.id.delete){

                        if (selectedIdList.size() > 0) {
                            deleteItems(selectedIdList);
                        }
                        mode.finish();
                        return true;
                    }
                    return false;
                }


                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    mode.getMenuInflater().inflate(R.menu.menu_main, menu);
                    actionMode = mode;
                    return true;
                }


                @Override
                public void onDestroyActionMode(ActionMode mode) {
                    actionMode = null;
                    if (selectedIdList.size() > 0) {
                        selectedIdList.clear();
                        adapter.mSelectedItemsIds.clear();
                    }
                }


                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    return false;
                }
            });

Any help will be highly appreciated, thanks in advance.

1

There are 1 best solutions below

1
On

You need to save selected items id in order to retain state and show as selected after you refresh/notify adapter changes like shown in below examples:

1) https://androidperspective.wordpress.com/2013/04/17/contextual-action-bar-with-listview/

(It shows selection and saving selected item ids in array to save state).

2) http://theopentutorials.com/examples/android/listview/android-contextual-action-bar-for-listview-item-deletion-using-actionbarsherlock/

(It shows complete example you want for deletion just using sherlock action bar it would be little change only).

Glad to know if it helps you.