Example usage for android.widget ListView getCheckedItemCount

List of usage examples for android.widget ListView getCheckedItemCount

Introduction

In this page you can find the example usage for android.widget ListView getCheckedItemCount.

Prototype

public int getCheckedItemCount() 

Source Link

Document

Returns the number of items currently selected.

Usage

From source file:cat.wuyingren.rorhelper.fragments.GameListFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    dataSource = new GameDataSource(getActivity());
    dataSource.open();/* w ww  .jav a 2s.c  o m*/

    values = dataSource.getAllGames();

    adapter = new MultipleRowAdapter(getActivity(), values);

    setListAdapter(adapter);

    final ListView listView = getListView();
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new ListView.MultiChoiceModeListener() {

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            // Here you can do something when items are selected/de-selected,
            // such as update the title in the CAB
            //mode.setTag();
            int selectionColor = getResources().getColor(R.color.colorPrimary);
            Log.w("TAG", "onItemCheckedStateChanged() " + checked + " " + position);
            mode.setSubtitle(listView.getCheckedItemCount() + " selected");
            if (checked) {
                listView.getChildAt(position).setBackgroundColor(selectionColor);
            } else {
                listView.getChildAt(position)
                        .setBackgroundColor(getResources().getColor(android.R.color.transparent));
            }
        }

        @Override
        public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
            // Inflate the menu for the CAB

            Log.w("TAG", "onCreateActionMode");
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.menu_context, menu);
            mode.setTitle(getString(R.string.action_choose));
            return true;
        }

        @Override
        public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
            // Here you can perform updates to the CAB due to
            // an invalidate() request
            return false;
        }

        @Override
        public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
            // Respond to clicks on the actions in the CAB

            switch (item.getItemId()) {
            case R.id.action_delete:
                deleteItems(listView.getCheckedItemPositions());
                mode.finish();
                return true;
            default:
                return false;
            }
        }

        @Override
        public void onDestroyActionMode(android.view.ActionMode mode) {
            // Here you can make any necessary updates to the activity when
            // the CAB is removed. By default, selected items are deselected/unchecked.

            SparseBooleanArray checked = listView.getCheckedItemPositions();
            for (int i = 0; i < listView.getAdapter().getCount(); i++) {
                if (checked.get(i)) {
                    listView.getChildAt(i)
                            .setBackgroundColor(getResources().getColor(android.R.color.transparent));
                }
            }
        }

    });
    // dataSource.close();
}