androidandroid-appcompatcontextual-action-bar

Contextual Action Bar using AppCompat


I am currently working on changing my app to use AppCompat version 21 so that I can include Material design into my app.

One of my activities has a list view, the class is extending ActionBarActivity.

As it is not a list activity, I am instead getting the list view using the findViewById and storing this in a variable so I can use it throughout the class for setting various aspect of the list view.

It was originally working, however, for some reason, since I've implemented the Toolbar to replace the Action Bar and using AppCompat, the multichoice no longer works. I can select a single item and everything else works its just the multiple choice that is broken.

Below is my code for getting the list view and setting up the click listeners and multi choice mode etc:

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            CommonTasks commonTasks = new CommonTasks(ConnectionManager.this);
            SharedPreferences settings = commonTasks.getAppsSharedPreferences();

            adView = (AdView) findViewById(R.id.adView);
            CommonTasks.getAddRequest(ConnectionManager.this, adView, settings.getBoolean(Defines.SharedPreferenceSettings.UPGRADE_PURCHASED, false));

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            swipeDetector = new SwipeDetector();
            listView = (ListView)findViewById(android.R.id.list);
            listView.setOnTouchListener(swipeDetector);
            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
            listView.setOnItemClickListener(mListItemClickListener);
            listView.setMultiChoiceModeListener(new ModeCallBack());

            listView.setOnItemClickListener(mItemClickListener);

private class ModeCallBack implements MultiChoiceModeListener
    {

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

            switch (item.getItemId())
            {
                case R.id.manageCon_mnuDeleteConnection:
                    deleteConnection(selectedConnectionName);
                    break;
                case R.id.manageCon_mnuEditConnection:
                    editConnection(selectedConnectionName);
                    break;
            }
            return false;
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {

            MenuInflater inflator = getMenuInflater();
            inflator.inflate(R.menu.manage_connections_context, menu);
            mode.setTitle("Change Connection");
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {

            return true;
        }

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position,
                long id, boolean checked) {
            if (!handlingDBSelection)
            {
                handlingDBSelection = true;
                selectedConnectionName = connectionArrayAdapter.getItem(position)
                        .toString();
                final int checkedCount = listView.getCheckedItemCount();
                switch (checkedCount)
                {
                    case 0:
                        mode.setSubtitle(null);
                        break;
                    default:
                        listView.clearChoices();
                        listView.setItemChecked(position, true);
                        break;
                }
                handlingDBSelection = false;
            }
        }

Solution

  • Why do you call listView.clearChoices() every time the item checked state changes? I don't see the whole code but it seems like you clear all of the checks and then just check that item.