List of usage examples for android.util SparseBooleanArray keyAt
public int keyAt(int index)
0...size()-1
, returns the key from the index
th key-value mapping that this SparseBooleanArray stores. From source file:org.totschnig.myexpenses.fragment.PartiesList.java
@Override public boolean dispatchCommandMultiple(int command, SparseBooleanArray positions, Long[] itemIds) { switch (command) { case R.id.DELETE_COMMAND: int columnIndexMappedTransactions = mPartiesCursor .getColumnIndex(DatabaseConstants.KEY_MAPPED_TRANSACTIONS); int columnIndexMappedTemplates = mPartiesCursor.getColumnIndex(DatabaseConstants.KEY_MAPPED_TEMPLATES); int columnIndexRowId = mPartiesCursor.getColumnIndex(DatabaseConstants.KEY_ROWID); int mappedTransactionsCount = 0, mappedTemplatesCount = 0; ArrayList<Long> idList = new ArrayList<>(); for (int i = 0; i < positions.size(); i++) { if (positions.valueAt(i)) { boolean deletable = true; mPartiesCursor.moveToPosition(positions.keyAt(i)); if (mPartiesCursor.getInt(columnIndexMappedTransactions) > 0) { mappedTransactionsCount++; deletable = false;/*ww w. j a v a 2s . c o m*/ } if (mPartiesCursor.getInt(columnIndexMappedTemplates) > 0) { mappedTemplatesCount++; deletable = false; } if (deletable) { idList.add(mPartiesCursor.getLong(columnIndexRowId)); } } } if (!idList.isEmpty()) { ((ProtectedFragmentActivity) getActivity()).startTaskExecution( TaskExecutionFragment.TASK_DELETE_PAYEES, idList.toArray(new Long[idList.size()]), null, R.string.progress_dialog_deleting); return true; } if (mappedTransactionsCount > 0 || mappedTemplatesCount > 0) { String message = ""; if (mappedTransactionsCount > 0) message += getResources().getQuantityString(R.plurals.not_deletable_mapped_transactions, mappedTransactionsCount, mappedTransactionsCount); if (mappedTemplatesCount > 0) message += getResources().getQuantityString(R.plurals.not_deletable_mapped_templates, mappedTemplatesCount, mappedTemplatesCount); Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show(); } break; } return super.dispatchCommandMultiple(command, positions, itemIds); }
From source file:com.android.contacts.SimImportFragment.java
private void importCurrentSelections() { final SparseBooleanArray checked = mListView.getCheckedItemPositions(); final ArrayList<SimContact> importableContacts = new ArrayList<>(checked.size()); for (int i = 0; i < checked.size(); i++) { // It's possible for existing contacts to be "checked" but we only want to import the // ones that don't already exist if (checked.valueAt(i) && !mAdapter.existsInCurrentAccount(i)) { importableContacts.add(mAdapter.getItem(checked.keyAt(i))); }//from w w w . ja v a2s .c o m } SimImportService.startImport(getContext(), mSubscriptionId, importableContacts, mAccountHeaderPresenter.getCurrentAccount()); }
From source file:com.rc.droid_stalker.components.NetworkStats.java
/** * Return list of unique UIDs known by this data structure. */// ww w . java2 s. c o m public int[] getUniqueUids() { final SparseBooleanArray uids = new SparseBooleanArray(); for (int uid : this.uid) { uids.put(uid, true); } final int size = uids.size(); final int[] result = new int[size]; for (int i = 0; i < size; i++) { result[i] = uids.keyAt(i); } return result; }
From source file:de.qspool.clementineremote.ui.fragments.LibraryFragment.java
@SuppressLint({ "InlinedApi", "NewApi" }) @Override//from ww w . j ava 2 s.c o m public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_library, container, false); mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.library_refresh_layout); mEmptyLibrary = (SwipeRefreshLayout) view.findViewById(R.id.library_refresh_empty_layout); mSwipeRefreshLayout.setOnRefreshListener(this); mEmptyLibrary.setOnRefreshListener(this); mSwipeRefreshLayout.setColorSchemeResources(R.color.orange); mEmptyLibrary.setColorSchemeResources(R.color.orange); mList = (ListView) view.findViewById(R.id.library); mLibraryEmptyText = (TextView) mEmptyLibrary.findViewById(R.id.library_empty_txt); mList.setOnItemClickListener(oiclLibraryClick); mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); mList.setMultiChoiceModeListener(new MultiChoiceModeListener() { @Override public boolean onActionItemClicked(ActionMode mode, android.view.MenuItem item) { SparseBooleanArray checkedPositions = mList.getCheckedItemPositions(); final LinkedList<SongSelectItem> selectedItems = new LinkedList<>(); final LinkedList<String> urls = new LinkedList<>(); mLibraryQueriesDone = 0; for (int i = 0; i < checkedPositions.size(); ++i) { int position = checkedPositions.keyAt(i); if (checkedPositions.valueAt(i)) { selectedItems.add(mAdapters.getLast().getItem(position)); } } for (SongSelectItem libraryItem : selectedItems) { OnSongSelectFinishedListener listener; switch (item.getItemId()) { case R.id.library_context_add: listener = new OnSongSelectFinishedListener() { @Override public void OnSongSelectFinished(LinkedList<SongSelectItem> l) { addSongsToPlaylist(l); } }; break; case R.id.library_context_download: listener = new OnSongSelectFinishedListener() { @Override public void OnSongSelectFinished(LinkedList<SongSelectItem> l) { for (SongSelectItem libItem : l) { urls.add(libItem.getUrl()); } mLibraryQueriesDone++; // Have we got all queries? if (mLibraryQueriesDone == selectedItems.size() && !urls.isEmpty()) { DownloadManager.getInstance() .addJob(ClementineMessageFactory.buildDownloadSongsMessage( ClementineRemoteProtocolBuffer.DownloadItem.Urls, urls)); } } }; break; default: return false; } queryLibraryItems(libraryItem, listener); } mode.finish(); return true; } @Override public boolean onCreateActionMode(ActionMode mode, android.view.Menu menu) { android.view.MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.library_context_menu, menu); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getActivity().getWindow() .setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.grey_cab_status)); } return true; } @Override public boolean onPrepareActionMode(ActionMode mode, android.view.Menu menu) { return false; } @Override public void onDestroyActionMode(ActionMode mode) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getActivity().getWindow() .setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.actionbar_dark)); } } @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { } }); createRootAdapter(); showList(); mActionBar.setTitle(""); mActionBar.setSubtitle("/"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); prefs.registerOnSharedPreferenceChangeListener(this); setHasOptionsMenu(true); return view; }
From source file:net.sourceforge.servestream.fragment.UrlListFragment.java
@Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); DragSortListView list = (DragSortListView) getListView(); list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); list.setDropListener(dropListener);/*from w w w . j a v a2 s.c o m*/ list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public synchronized void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (mActionMode == null) { UriBean uriBean = (UriBean) parent.getItemAtPosition(position); processUri(uriBean.getUri().toString()); } else { for (int i = 0; i < mChecked.size(); i++) { if (mChecked.keyAt(i) == position && mChecked.valueAt(i)) { view.setBackgroundResource(0); getListView().setItemChecked(position, false); mAdapter.notifyDataSetChanged(); } } // Notice how the ListView api is lame // You can use mListView.getCheckedItemIds() if the adapter // has stable ids, e.g you're using a CursorAdaptor SparseBooleanArray checked = getListView().getCheckedItemPositions(); boolean hasCheckedElement = false; checked = getListView().getCheckedItemPositions(); for (int i = 0; i < checked.size(); i++) { if (checked.valueAt(i)) { View v = getListView().getChildAt(checked.keyAt(i)); if (v != null) { v.setBackgroundColor(getActivity().getResources() .getColor(R.color.selection_background_color_light)); } } } mAdapter.notifyDataSetChanged(); for (int i = 0; i < checked.size() && !hasCheckedElement; i++) { hasCheckedElement = checked.valueAt(i); } if (hasCheckedElement) { if (mActionMode == null) { mActionMode = ((ActionBarActivity) getActivity()) .startSupportActionMode(mActionModeCallback); } } else { if (mActionMode != null) { mActionMode.finish(); mActionMode = null; } } mChecked = getListView().getCheckedItemPositions().clone(); } } }); list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { if (mActionMode == null) { mActionMode = ((ActionBarActivity) getActivity()).startSupportActionMode(mActionModeCallback); getListView().setItemChecked(position, true); view.setBackgroundColor( getActivity().getResources().getColor(R.color.selection_background_color_light)); mChecked = getListView().getCheckedItemPositions().clone(); return true; } return false; } }); DragSortController controller = new DragSortController(list); controller.setDragInitMode(DragSortController.ON_DRAG); controller.setDragHandleId(R.id.drag_handle); list.setOnTouchListener(controller); mAdapter = new UrlListAdapter(getActivity(), new ArrayList<UriBean>(), this); setListAdapter(mAdapter); }
From source file:org.mariotaku.twidere.fragment.AddStatusFilterDialogFragment.java
@NonNull @Override//from w ww.j a v a 2 s .c o m public Dialog onCreateDialog(final Bundle savedInstanceState) { final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); mFilterItems = getFilterItemsInfo(); final String[] entries = new String[mFilterItems.length]; final boolean nameFirst = mPreferences.getBoolean(KEY_NAME_FIRST); for (int i = 0, j = entries.length; i < j; i++) { final FilterItemInfo info = mFilterItems[i]; switch (info.type) { case FilterItemInfo.FILTER_TYPE_USER: entries[i] = getString(R.string.user_filter_name, getName(mUserColorNameManager, info.value, nameFirst)); break; case FilterItemInfo.FILTER_TYPE_KEYWORD: entries[i] = getString(R.string.keyword_filter_name, getName(mUserColorNameManager, info.value, nameFirst)); break; case FilterItemInfo.FILTER_TYPE_SOURCE: entries[i] = getString(R.string.source_filter_name, getName(mUserColorNameManager, info.value, nameFirst)); break; } } builder.setTitle(R.string.add_to_filter); builder.setMultiChoiceItems(entries, null, null); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { AlertDialog alertDialog = ((AlertDialog) dialog); final SparseBooleanArray checkPositions = alertDialog.getListView().getCheckedItemPositions(); final Set<UserKey> userKeys = new HashSet<>(); final Set<String> keywords = new HashSet<>(); final Set<String> sources = new HashSet<>(); final ArrayList<ContentValues> userValues = new ArrayList<>(); final ArrayList<ContentValues> keywordValues = new ArrayList<>(); final ArrayList<ContentValues> sourceValues = new ArrayList<>(); for (int i = 0, j = checkPositions.size(); i < j; i++) { if (!checkPositions.valueAt(i)) continue; final FilterItemInfo info = mFilterItems[checkPositions.keyAt(i)]; final Object value = info.value; if (value instanceof ParcelableUserMention) { final ParcelableUserMention mention = (ParcelableUserMention) value; userKeys.add(mention.key); userValues.add(ContentValuesCreator.createFilteredUser(mention)); } else if (value instanceof UserItem) { final UserItem item = (UserItem) value; userKeys.add(item.key); userValues.add(createFilteredUser(item)); } else if (info.type == FilterItemInfo.FILTER_TYPE_KEYWORD) { if (value != null) { final String keyword = ParseUtils.parseString(value); keywords.add(keyword); final ContentValues values = new ContentValues(); values.put(Filters.Keywords.VALUE, "#" + keyword); keywordValues.add(values); } } else if (info.type == FilterItemInfo.FILTER_TYPE_SOURCE) { if (value != null) { final String source = ParseUtils.parseString(value); sources.add(source); final ContentValues values = new ContentValues(); values.put(Filters.Sources.VALUE, source); sourceValues.add(values); } } } final ContentResolver resolver = getContentResolver(); ContentResolverUtils.bulkDelete(resolver, Filters.Users.CONTENT_URI, Filters.Users.USER_KEY, userKeys, null); ContentResolverUtils.bulkDelete(resolver, Filters.Keywords.CONTENT_URI, Filters.Keywords.VALUE, keywords, null); ContentResolverUtils.bulkDelete(resolver, Filters.Sources.CONTENT_URI, Filters.Sources.VALUE, sources, null); ContentResolverUtils.bulkInsert(resolver, Filters.Users.CONTENT_URI, userValues); ContentResolverUtils.bulkInsert(resolver, Filters.Keywords.CONTENT_URI, keywordValues); ContentResolverUtils.bulkInsert(resolver, Filters.Sources.CONTENT_URI, sourceValues); } }); builder.setNegativeButton(android.R.string.cancel, null); return builder.create(); }
From source file:com.github.jobs.ui.dialog.RemoveServicesDialog.java
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { final ArrayList<Parcelable> services = getArguments().getParcelableArrayList(ARG_SERVICES); CharSequence[] items = new CharSequence[services.size()]; for (int i = 0, servicesSize = services.size(); i < servicesSize; i++) { TemplateService service = (TemplateService) services.get(i); items[i] = getString(R.string.service_list_item, service.getType(), service.getData()); }/* w ww . j ava 2 s . c om*/ final SparseBooleanArray checked = new SparseBooleanArray(); return new AlertDialog.Builder(getActivity()).setTitle(R.string.title_remove_services) .setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checked.put(which, isChecked); } }).setPositiveButton(R.string.remove_selected_services, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // get a list of the services to delete ArrayList<TemplateService> toRemove = new ArrayList<TemplateService>(); for (int i = 0; i < checked.size(); i++) { if (checked.valueAt(i)) { int index = checked.keyAt(i); toRemove.add((TemplateService) services.get(index)); } } // fire service deletion FragmentActivity activity = getActivity(); if (activity instanceof EditTemplateActivity) { EditTemplateActivity editTemplateActivity = (EditTemplateActivity) activity; editTemplateActivity.removeServices(toRemove); } } }).setNegativeButton(R.string.cancel, null).create(); }
From source file:net.naonedbus.fragment.impl.FavorisFragment.java
private List<Integer> getCheckedItemsIds() { final List<Integer> ids = new ArrayList<Integer>(); final SparseBooleanArray checked = mListView.getCheckedItemPositions(); for (int i = 0; i < checked.size(); i++) { if (checked.valueAt(i)) { final Favori item = (Favori) mListView.getItemAtPosition(checked.keyAt(i)); ids.add(item.getId());// w w w.j av a 2s . c o m } } return ids; }
From source file:net.naonedbus.fragment.impl.FavorisFragment.java
private Favori getFirstSelectedItem() { final SparseBooleanArray checkedPositions = mListView.getCheckedItemPositions(); for (int i = 0; i < checkedPositions.size(); i++) { if (checkedPositions.valueAt(i)) { return (Favori) mListView.getItemAtPosition(checkedPositions.keyAt(i)); }// www .j av a2 s .c om } return null; }
From source file:org.dvbviewer.controller.ui.fragments.TimerList.java
@Override public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: SparseBooleanArray checkedPositions = getListView().getCheckedItemPositions(); Log.i(RecordingList.class.getSimpleName(), "items selected: " + checkedPositions.size()); if (checkedPositions != null && checkedPositions.size() > 0) { int size = checkedPositions.size(); TimerDeleter deleter = new TimerDeleter(TimerList.this); List<Timer> timers = new ArrayList<Timer>(); for (int i = 0; i < size; i++) { if (checkedPositions.valueAt(i)) { timers.add(mAdapter.getItem(checkedPositions.keyAt(i))); }//from ww w . j a va2 s.co m } Timer[] array = new Timer[timers.size()]; deleter.execute(timers.toArray(array)); } mode.finish(); break; case DialogInterface.BUTTON_NEGATIVE: // No button clicked break; } }