Android CAB destroying after listview notifydatasetchanged
Asked Answered
C

1

9

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.

Comprador answered 24/10, 2016 at 13:50 Comment(3)
try to remove mode.finish() from itemclick that may be prevent from recreate.Ernest
please add log here so can review itErnest
@parikdhakan mode.finish() in onActionItemClicked is called on tap of delete icon in actionbar, otherwise it will not be called there.Comprador
F
0

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.

Fascination answered 3/11, 2016 at 4:44 Comment(1)
The selection is not a problem as I have mentioned that the items remains selected, so I can get them. The problem is while list refreshes the CAB disappears (sometimes twice) and then it becomes visible. While debugging one can easily see that onCreateActionMode is called again during list refreshes. If you are using Gmail you can see that the CAB doesn't destroy and recreated while loading more data.Comprador

© 2022 - 2024 — McMap. All rights reserved.