Example usage for android.util SparseBooleanArray get

List of usage examples for android.util SparseBooleanArray get

Introduction

In this page you can find the example usage for android.util SparseBooleanArray get.

Prototype

public boolean get(int key) 

Source Link

Document

Gets the boolean mapped from the specified key, or false if no such mapping has been made.

Usage

From source file:android.databinding.ViewDataBinding.java

/** @hide */
protected static boolean getFromList(SparseBooleanArray list, int index) {
    if (list == null || index < 0) {
        return false;
    }/*from   w w w.  j  ava 2  s .co  m*/
    return list.get(index);
}

From source file:com.veniosg.dir.android.fragment.SideNavFragment.java

private void setListChoiceListener() {
    getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override//from  www  .  j  av a  2  s.c o  m
        public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
            menu.clear();
            mode.getMenuInflater().inflate(R.menu.cab_bookmarks, menu);

            if (getListView().getCheckedItemCount() != 1) {
                menu.removeItem(R.id.menu_open_parent);
            }
            return true;
        }

        @Override
        public void onDestroyActionMode(android.view.ActionMode mode) {
            setStatusBarColour(getActivity(), false);
        }

        @Override
        public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
            setStatusBarColour(getActivity(), true);
            return true;
        }

        @Override
        public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
            case R.id.menu_delete:
                long[] ids = getListView().getCheckedItemIds();
                for (long id : ids) {
                    getListView().getContext().getContentResolver().delete(BookmarkProvider.CONTENT_URI,
                            BookmarkProvider._ID + "=?", new String[] { "" + id });
                }
                mode.finish();
                return true;

            case R.id.menu_open_parent:
                int pos = 0;
                SparseBooleanArray checked = getListView().getCheckedItemPositions();
                for (int i = 0; i < getListView().getCount(); i++) {
                    if (checked.get(i)) {
                        pos = i;
                    }
                }

                String path = ((Cursor) getListAdapter().getItem(pos)).getString(2);
                ((BookmarkContract) getActivity()).onBookmarkSelected(new File(path).getParent());
                mode.finish();
                return true;
            }
            return false;
        }

        @Override
        public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id,
                boolean checked) {
            if (getListView().getCheckedItemCount() != 0) {

                mode.setTitle(getListView().getCheckedItemCount() + " " + getString(R.string.selected));

                // Force actions' refresh
                mode.invalidate();
            }
        }
    });
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
}

From source file:org.mozilla.gecko.RemoteClientsDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final String title = getArguments().getString(KEY_TITLE);
    final String positiveButtonText = getArguments().getString(KEY_POSITIVE_BUTTON_TEXT);
    final ChoiceMode choiceMode = ChoiceMode.values()[getArguments().getInt(KEY_CHOICE_MODE)];
    final ArrayList<RemoteClient> clients = getArguments().getParcelableArrayList(KEY_CLIENTS);

    final Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(title);/*from   w w  w  . j av a2  s .  com*/

    final String[] clientNames = new String[clients.size()];
    for (int i = 0; i < clients.size(); i++) {
        clientNames[i] = clients.get(i).name;
    }

    if (choiceMode == ChoiceMode.MULTIPLE) {
        builder.setMultiChoiceItems(clientNames, null, null);
        builder.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                if (which != Dialog.BUTTON_POSITIVE) {
                    return;
                }

                final AlertDialog dialog = (AlertDialog) dialogInterface;
                final SparseBooleanArray checkedItemPositions = dialog.getListView().getCheckedItemPositions();
                final ArrayList<RemoteClient> checked = new ArrayList<RemoteClient>();
                for (int i = 0; i < clients.size(); i++) {
                    if (checkedItemPositions.get(i)) {
                        checked.add(clients.get(i));
                    }
                }
                notifyListener(checked);
            }
        });
    } else {
        builder.setItems(clientNames, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int index) {
                final ArrayList<RemoteClient> checked = new ArrayList<RemoteClient>();
                checked.add(clients.get(index));
                notifyListener(checked);
            }
        });
    }

    return builder.create();
}

From source file:com.test.weeklly.gplus.ListPeopleActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.person_list_activity);

    mListItems = new ArrayList<String>();
    mListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,
            mListItems);/*from   w  w  w  . ja  v  a 2  s  . co  m*/
    mPersonListView = (ListView) findViewById(R.id.person_list);
    mPersonListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    mPersonListView.setAdapter(mListAdapter);
    mPlusClientFragment = PlusClientFragment.getPlusClientFragment(this, MomentUtil.VISIBLE_ACTIVITIES);
    done = (Button) findViewById(R.id.done);
    done.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String str = "";
            SparseBooleanArray checked = mPersonListView.getCheckedItemPositions();
            int size = checked.size(); // number of name-value pairs in the array
            for (int i = 0; i < size; i++) {
                int key = checked.keyAt(i);
                boolean value = checked.get(key);
                if (value) {
                    str += "\n" + mListItems.get(key);
                    done.setText(str);
                    Intent returnIntent = new Intent();
                    returnIntent.putExtra("result", str);
                    setResult(RESULT_OK, returnIntent);
                    finish();

                }
                //doSomethingWithSelectedIndex(key);
            }
        }
    });
}

From source file:ca.rmen.android.networkmonitor.app.prefs.SelectFieldsActivity.java

public void onOk(@SuppressWarnings("UnusedParameters") View v) {
    Log.v(TAG, "onOk");
    SparseBooleanArray checkedPositions = mListView.getCheckedItemPositions();
    String[] dbColumns = NetMonColumns.getColumnNames(this);
    final List<String> selectedColumns = new ArrayList<>(dbColumns.length);
    for (int i = 0; i < dbColumns.length; i++) {
        if (checkedPositions.get(i))
            selectedColumns.add(dbColumns[i]);
    }//from   w  ww  .  ja  v  a 2s  . co  m
    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            NetMonPreferences.getInstance(SelectFieldsActivity.this).setSelectedColumns(selectedColumns);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            setResult(Activity.RESULT_OK);
            finish();
        }
    }.execute();
}

From source file:me.diskstation.ammon.gpsrunner.ui.CalendarFragment.java

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    //Run selectedRun = (Run) parent.getItemAtPosition(position);
    //Run testRun = (Run) parent.getSelectedItem();
    ArrayList<Long> selectedIds = new ArrayList<>();
    SparseBooleanArray selections = mListView.getCheckedItemPositions();
    for (int i = 0; i <= mAdapter.getCount(); i++) {
        if (selections.get(i)) {
            Run selectedRun = (Run) mListView.getItemAtPosition(i);
            selectedIds.add(selectedRun.id);
        }//from  w  w  w.  j  av  a  2 s .  c o m
    }
    long[] runIds = new long[selectedIds.size()];
    for (int i = 0; i < selectedIds.size(); i++) {
        runIds[i] = selectedIds.get(i);
    }

    switch (item.getItemId()) {
    case R.id.item_open:
        //Call DetailsTabActivity with runIds = id;
        Intent intent = new Intent(getActivity(), DetailsTabActivity.class);
        intent.putExtra("run_ids", runIds);
        startActivity(intent);
        mode.finish();
        return true;
    case R.id.item_export_csv:

        exportRunIds = new Long[runIds.length];
        for (int i = 0; i < exportRunIds.length; i++) {
            exportRunIds[i] = runIds[i];
        }
        if (ContextCompat.checkSelfPermission(getActivity(),
                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

            exportRuns();
        } else {
            requestPermissions(new String[] { Manifest.permission.READ_CONTACTS },
                    WRITE_EXTERNAL_STORAGE_REQUEST);

        }
        mode.finish();
        return true;
    default:
        return false;
    }
}

From source file:com.theelix.libreexplorer.ActionModeCallback.java

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    SparseBooleanArray checkedArray = listView.getCheckedItemPositions();
    final List<File> selectedFiles = new ArrayList<>();
    final ArrayList<Uri> selectedUris = new ArrayList<>();
    for (int i = 0; i < listView.getCount(); i++) {
        if (checkedArray.get(i)) {
            selectedFiles.add((File) listView.getItemAtPosition(i));
            selectedUris.add(android.net.Uri.fromFile((File) listView.getItemAtPosition(i)));
        }//from www  .j  ava 2  s  .  c  o  m
    }

    switch (item.getItemId()) {
    case R.id.toolbar_menu_copy:
        FileManager.prepareCopy(selectedFiles.toArray(new File[selectedFiles.size()]));
        break;
    case R.id.toolbar_menu_cut:
        FileManager.prepareCut(selectedFiles.toArray(new File[selectedFiles.size()]));
        break;
    case R.id.toolbar_menu_delete:
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        if (selectedFiles.size() > 1) {
            builder.setMessage(mContext.getString(R.string.delete_dialog_multiple) + " " + selectedFiles.size()
                    + " " + mContext.getString(R.string.delete_dialog_items) + "?");
        } else
            builder.setMessage(mContext.getString(R.string.delete_dialog_single));

        builder.setPositiveButton(mContext.getString(R.string.dialog_ok),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        DeleteTask task = new DeleteTask(mContext,
                                selectedFiles.toArray(new File[selectedFiles.size()]));
                        task.execute();

                    }
                });
        builder.setNegativeButton(mContext.getString(R.string.dialog_cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        builder.show();
        break;
    case R.id.toolbar_menu_rename:
        DialogFragment renameDialog = new RenameDialog();
        ((RenameDialog) renameDialog).setSelectedFiles(selectedFiles.toArray());
        renameDialog.show(((FileManagerActivity) mContext).getSupportFragmentManager(), "rename_dialog");
        break;
    case R.id.toolbar_menu_details:
        DialogFragment detailsDialog = new DetailsDialog();
        ((DetailsDialog) detailsDialog).setTargetFile(selectedFiles.get(0));
        detailsDialog.show(((FileManagerActivity) mContext).getSupportFragmentManager(), "details_dialog");
        break;
    case R.id.toolbar_menu_share:
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, selectedUris);
        shareIntent.setType("*/*");
        mContext.startActivity(Intent.createChooser(shareIntent, "Share to..."));
        break;

    }
    mode.finish();
    return true;
}

From source file:com.nearnotes.NoteList.java

@Override
public void onStart() {
    super.onStart();

    mCallback.setActionItems(NOTE_LIST);
    mSelectedIds = new ArrayList<Long>();
    mDbHelper = new NotesDbAdapter(getActivity()); // Create new custom database class for sqlite and pass the current context as a variable
    mDbHelper.open(); // Gets the writable database

    Bundle bundle = getArguments();//from   w  ww . ja  va 2  s  .c  om
    mLongitude = bundle.getDouble("longitude");
    mLatitude = bundle.getDouble("latitude");
    fillData(mLongitude, mLatitude);
    mListView = getListView();

    getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);

    getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {

        private int nr = 0;

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

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActionModeFlag = false;
            Log.e("onDestroyActionMode", "onDestroyActionMode");
            mAdapter.clearSelection();
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mActionModeFlag = true;

            nr = 0;
            MenuInflater inflater = getActivity().getMenuInflater();
            inflater.inflate(R.menu.contextual_menu, menu);
            return true;
        }

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

            case R.id.context_delete:
                for (Long s : mSelectedIds) {
                    mDbHelper.deleteNote(s);
                    if (mDbHelper.fetchSetting() == s) {
                        mDbHelper.removeSetting();
                    }
                }
                nr = 0;
                mAdapter.clearSelection();
                fillData(mLongitude, mLatitude);
                mode.finish();

                return true;
            case R.id.context_select_all:
                SparseBooleanArray checkSparse = getListView().getCheckedItemPositions();
                for (int i = 0; i < mListView.getCount(); i++) {
                    if (!checkSparse.get(i)) {
                        getListView().setItemChecked(i, true);

                    }
                }
                return true;
            }
            return false;
        }

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            Log.e("checked", String.valueOf(checked) + " id: " + String.valueOf(id) + " position: "
                    + String.valueOf(position));
            if (checked) {
                nr++;
                mSelectedIds.add(id);
                mAdapter.setNewSelection(position, checked);
            } else {
                mSelectedIds.remove(id);
                nr--;
                mAdapter.removeSelection(position);
            }

            TextView tv = (TextView) getActivity().getLayoutInflater().inflate(R.layout.contextual_title, null);
            tv.setText(nr + " selected");
            mode.setCustomView(tv);

        }
    });

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            getListView().setItemChecked(position, !mAdapter.isPositionChecked(position));
            return false;
        }
    });

}

From source file:com.github.runoshun.in_app_survey.ui.QuestionFragment.java

private void onMultiChoiceItemClick(MultiChoiceQuestion question, ListView listView, int position) {
    SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
    List<Integer> selectedPositions = new ArrayList<>();

    int count = listView.getAdapter().getCount();
    for (int i = 0; i < count; ++i) {
        if (checkedItems.get(i)) {
            selectedPositions.add(i);/*from  w  w w.j  a  v a2s  . c  o  m*/
        }
    }

    if (selectedPositions.size() > question.maxChoices) {
        listView.setItemChecked(position, false);
        selectedPositions.remove((Integer) position);
    }

    int[] selected = new int[selectedPositions.size()];
    for (int i = 0; i < selected.length; ++i) {
        selected[i] = selectedPositions.get(i);
    }

    if (selectedPositions.size() >= question.minChoices) {
        question.saveAnswer(selected);
    } else {
        question.saveAnswer(null);
    }
}

From source file:com.btmura.android.reddit.app.AccountListFragment.java

private void handleDelete(ActionMode mode) {
    SparseBooleanArray checked = getListView().getCheckedItemPositions();
    int checkedCount = getListView().getCheckedItemCount();
    String[] accountNames = new String[checkedCount];
    int count = adapter.getCount();
    int j = 0;/*from w  w  w  .j  ava2s.co m*/
    for (int i = 0; i < count; i++) {
        if (checked.get(i)) {
            accountNames[j++] = adapter.getItem(i).getAccountName();
        }
    }
    removeAccounts(accountNames);
    mode.finish();
}