下面列出了android.util.SparseBooleanArray#valueAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private int[] getSelectedIndices(ListView listView) {
SparseBooleanArray sparseArray = listView.getCheckedItemPositions();
int selectedCount = 0;
for (int i = 0; i < sparseArray.size(); ++i) {
if (sparseArray.valueAt(i)) {
selectedCount++;
}
}
int[] indices = new int[selectedCount];
for (int i = 0, j = 0; i < sparseArray.size(); ++i) {
if (sparseArray.valueAt(i)) {
indices[j++] = sparseArray.keyAt(i);
}
}
return indices;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// Check to see what the new checked state is, and then notify the listener
final boolean checked = mListView.isItemChecked(position);
mListener.onItemCheckedStateChanged(mActionMode, position, id, checked);
boolean hasCheckedItem = checked;
// Check to see if we have any checked items
if (!hasCheckedItem) {
SparseBooleanArray checkedItemPositions = mListView.getCheckedItemPositions();
if (checkedItemPositions != null) {
// Iterate through the SparseBooleanArray to see if there is a checked item
int i = 0;
while (!hasCheckedItem && i < checkedItemPositions.size()) {
hasCheckedItem = checkedItemPositions.valueAt(i++);
}
}
}
// If we don't have any checked items, finish the action mode
if (!hasCheckedItem) {
mActionMode.finish();
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// Check to see what the new checked state is, and then notify the listener
final boolean checked = mListView.isItemChecked(position);
mListener.onItemCheckedStateChanged(mActionMode, position, id, checked);
boolean hasCheckedItem = checked;
// Check to see if we have any checked items
if (!hasCheckedItem) {
SparseBooleanArray checkedItemPositions = mListView.getCheckedItemPositions();
if (checkedItemPositions != null) {
// Iterate through the SparseBooleanArray to see if there is a checked item
int i = 0;
while (!hasCheckedItem && i < checkedItemPositions.size()) {
hasCheckedItem = checkedItemPositions.valueAt(i++);
}
}
}
// If we don't have any checked items, finish the action mode
if (!hasCheckedItem) {
mActionMode.finish();
}
}
@Override
public BuildConfig.ORM[] getSelectedValues() {
final BuildConfig.ORM[] orms = BuildConfig.ORM.values();
final SparseBooleanArray array = getSelectedPositions();
final List<BuildConfig.ORM> list = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
if (array.valueAt(i)) {
list.add(orms[array.keyAt(i)]);
}
}
final BuildConfig.ORM[] selected = new BuildConfig.ORM[list.size()];
list.toArray(selected);
return selected;
}
private void deleteSelectedItems(){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean delNotes = sp.getBoolean(SettingsActivity.PREF_DEL_NOTES, false);
CursorAdapter adapter = (CursorAdapter) list.getAdapter();
SparseBooleanArray checkedItemPositions = list.getCheckedItemPositions();
for (int i=0; i < checkedItemPositions.size(); i++) {
if(checkedItemPositions.valueAt(i)) {
if (delNotes) {
DbAccess.trashNotesByCategoryId(getBaseContext(), (int) (long) adapter.getItemId(checkedItemPositions.keyAt(i)));
}
DbAccess.deleteCategory(getBaseContext(), (int) (long) adapter.getItemId(checkedItemPositions.keyAt(i)));
}
}
}
/**
* Returns the set of checked items ids. The result is only valid if the
* choice mode has not been set to {@link #CHOICE_MODE_NONE}.
*
* @return A new array which contains the id of each checked item in the
* list.
*
* @deprecated Use {@link #getCheckedItemIds()} instead.
*/
@Deprecated
public long[] getCheckItemIds() {
// Use new behavior that correctly handles stable ID mapping.
if (mAdapter != null && mAdapter.hasStableIds()) {
return getCheckedItemIds();
}
// Old behavior was buggy, but would sort of work for adapters without stable IDs.
// Fall back to it to support legacy apps.
if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null) {
final SparseBooleanArray states = mCheckStates;
final int count = states.size();
final long[] ids = new long[count];
final ListAdapter adapter = mAdapter;
int checkedCount = 0;
for (int i = 0; i < count; i++) {
if (states.valueAt(i)) {
ids[checkedCount++] = adapter.getItemId(states.keyAt(i));
}
}
// Trim array if needed. mCheckStates may contain false values
// resulting in checkedCount being smaller than count.
if (checkedCount == count) {
return ids;
} else {
final long[] result = new long[checkedCount];
System.arraycopy(ids, 0, result, 0, checkedCount);
return result;
}
}
return new long[0];
}
private static int findFirstSetIndex(SparseBooleanArray sba, int rangeStart, int rangeEnd) {
int size = sba.size();
int i = insertionIndexForKey(sba, rangeStart);
while (i < size && sba.keyAt(i) < rangeEnd && !sba.valueAt(i))
i++;
if (i == size || sba.keyAt(i) >= rangeEnd)
return -1;
return i;
}
private void uncheckAllItems() {
if( listView == null ) {
return;
}
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i)) {
listView.setItemChecked(position, false);
}
}
}
/**
* Returns the set of checked items ids. The result is only valid if the choice mode has not been set to
* {@link #CHOICE_MODE_NONE}.
*
* @return A new array which contains the id of each checked item in the list.
*
* @deprecated Use {@link #getCheckedItemIds()} instead.
*/
@Deprecated
public long[] getCheckItemIds() {
// Use new behavior that correctly handles stable ID mapping.
if ( mAdapter != null && mAdapter.hasStableIds() ) {
return getCheckedItemIds();
}
// Old behavior was buggy, but would sort of work for adapters without stable IDs.
// Fall back to it to support legacy apps.
if ( mChoiceMode != ListView.CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null ) {
final SparseBooleanArray states = mCheckStates;
final int count = states.size();
final long[] ids = new long[count];
final ListAdapter adapter = mAdapter;
int checkedCount = 0;
for ( int i = 0; i < count; i++ ) {
if ( states.valueAt( i ) ) {
ids[checkedCount++] = adapter.getItemId( states.keyAt( i ) );
}
}
// Trim array if needed. mCheckStates may contain false values
// resulting in checkedCount being smaller than count.
if ( checkedCount == count ) {
return ids;
} else {
final long[] result = new long[checkedCount];
System.arraycopy( ids, 0, result, 0, checkedCount );
return result;
}
}
return new long[0];
}
private void deleteSelectedItems(){
ArrayAdapter adapter = (ArrayAdapter) lvItemList.getAdapter();
SparseBooleanArray checkedItemPositions = lvItemList.getCheckedItemPositions();
ArrayList<CheckListItem> temp = new ArrayList<>();
for (int i=0; i < checkedItemPositions.size(); i++) {
if(checkedItemPositions.valueAt(i)) {
temp.add((CheckListItem) adapter.getItem(checkedItemPositions.keyAt(i)));
}
}
if (temp.size() > 0) {
itemNamesList.removeAll(temp);
}
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_REGISTER_LISTENER:
addListener((ITrustListener) msg.obj);
break;
case MSG_UNREGISTER_LISTENER:
removeListener((ITrustListener) msg.obj);
break;
case MSG_DISPATCH_UNLOCK_ATTEMPT:
dispatchUnlockAttempt(msg.arg1 != 0, msg.arg2);
break;
case MSG_DISPATCH_UNLOCK_LOCKOUT:
dispatchUnlockLockout(msg.arg1, msg.arg2);
break;
case MSG_ENABLED_AGENTS_CHANGED:
refreshAgentList(UserHandle.USER_ALL);
// This is also called when the security mode of a user changes.
refreshDeviceLockedForUser(UserHandle.USER_ALL);
break;
case MSG_KEYGUARD_SHOWING_CHANGED:
refreshDeviceLockedForUser(mCurrentUser);
break;
case MSG_START_USER:
case MSG_CLEANUP_USER:
case MSG_UNLOCK_USER:
refreshAgentList(msg.arg1);
break;
case MSG_SWITCH_USER:
mCurrentUser = msg.arg1;
refreshDeviceLockedForUser(UserHandle.USER_ALL);
break;
case MSG_STOP_USER:
setDeviceLockedForUser(msg.arg1, true);
break;
case MSG_FLUSH_TRUST_USUALLY_MANAGED:
SparseBooleanArray usuallyManaged;
synchronized (mTrustUsuallyManagedForUser) {
usuallyManaged = mTrustUsuallyManagedForUser.clone();
}
for (int i = 0; i < usuallyManaged.size(); i++) {
int userId = usuallyManaged.keyAt(i);
boolean value = usuallyManaged.valueAt(i);
if (value != mLockPatternUtils.isTrustUsuallyManaged(userId)) {
mLockPatternUtils.setTrustUsuallyManaged(value, userId);
}
}
break;
case MSG_REFRESH_DEVICE_LOCKED_FOR_USER:
refreshDeviceLockedForUser(msg.arg1);
break;
}
}
private static int buildRunList(SparseBooleanArray cip, int rangeStart, int rangeEnd,
int[] runStart, int[] runEnd) {
int runCount = 0;
int i = findFirstSetIndex(cip, rangeStart, rangeEnd);
if (i == -1)
return 0;
int position = cip.keyAt(i);
int currentRunStart = position;
int currentRunEnd = currentRunStart + 1;
for (i++; i < cip.size() && (position = cip.keyAt(i)) < rangeEnd; i++) {
if (!cip.valueAt(i)) // not checked => not interesting
continue;
if (position == currentRunEnd) {
currentRunEnd++;
} else {
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
currentRunStart = position;
currentRunEnd = position + 1;
}
}
if (currentRunEnd == rangeEnd) {
// rangeStart and rangeEnd are equivalent positions so to be
// consistent we translate them to the same integer value. That way
// we can check whether a run covers the entire range by just
// checking if the start equals the end position.
currentRunEnd = rangeStart;
}
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
if (runCount > 1) {
if (runStart[0] == rangeStart && runEnd[runCount - 1] == rangeStart) {
// The last run ends at the end of the range, and the first run
// starts at the beginning of the range. So they are actually
// part of the same run, except they wrap around the end of the
// range. To avoid adjacent runs, we need to merge them.
runStart[0] = runStart[runCount - 1];
runCount--;
}
}
return runCount;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// retrieve selected items and print them out
ScanAdapter adapter = (ScanAdapter) ScansActivity.this.getListAdapter();
SparseBooleanArray selected = adapter.getSelectedIds();
if (item.getItemId() == R.id.compare_item)
{
if (selected.size() == 2)
{
Scan scanOne = adapter.getItem(selected.keyAt(0));
Scan scanTwo = adapter.getItem(selected.keyAt(1));
ScanDiff diff = ScanDiff.compare(scanOne, scanTwo);
showDiffReport(diff);
}
else
{
Toast.makeText(ScansActivity.this, "Please select two scans to compare", Toast.LENGTH_LONG).show();
}
}
else if (item.getItemId() == R.id.share_item)
{
for (int i = 0; i < selected.size(); i++){
if (selected.valueAt(i)) {
Scan selectedItem = adapter.getItem(selected.keyAt(i));
String shareText = selectedItem.toString();
shareText(shareText);
}
}
}
// close action mode
mode.finish();
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Calls getSelectedIds method from ListViewAdapter Class
SparseBooleanArray selected = history.historyListAdapter
.getSelectedIds();
// Captures all selected ids with a loop
List<Integer> selectedRecordIds = new ArrayList<Integer>();
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
selectedRecordIds.add((int)history.historyListAdapter.getItemId(selected.keyAt(i)));
}
}
switch (item.getItemId()) {
case R.id.delete:
if(!selectedRecordIds.isEmpty()) {
// Remove selected items following the ids
history.historyListAdapter.remove(selectedRecordIds);
}
// Close CAB
mode.finish();
return true;
case R.id.publish:
boolean deleted = false;
if(!selectedRecordIds.isEmpty()) {
for(Integer recordId : new ArrayList<Integer>(selectedRecordIds)) {
Storage.Record record = history.measurementManager.getRecord(recordId);
if (!record.getUploadId().isEmpty()) {
selectedRecordIds.remove(recordId);
deleted = true;
}
}
if(deleted) {
Toast.makeText(history, history.getString(R.string.history_already_uploaded), Toast.LENGTH_LONG).show();
}
// publish selected items following the ids
history.doTransferRecords(selectedRecordIds);
}
// Close CAB
mode.finish();
return true;
default:
return false;
}
}
private static int buildRunList(SparseBooleanArray cip, int rangeStart,
int rangeEnd, int[] runStart, int[] runEnd) {
int runCount = 0;
int i = findFirstSetIndex(cip, rangeStart, rangeEnd);
if (i == -1)
return 0;
int position = cip.keyAt(i);
int currentRunStart = position;
int currentRunEnd = currentRunStart + 1;
for (i++; i < cip.size() && (position = cip.keyAt(i)) < rangeEnd; i++) {
if (!cip.valueAt(i)) // not checked => not interesting
continue;
if (position == currentRunEnd) {
currentRunEnd++;
} else {
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
currentRunStart = position;
currentRunEnd = position + 1;
}
}
if (currentRunEnd == rangeEnd) {
// rangeStart and rangeEnd are equivalent positions so to be
// consistent we translate them to the same integer value. That way
// we can check whether a run covers the entire range by just
// checking if the start equals the end position.
currentRunEnd = rangeStart;
}
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
if (runCount > 1) {
if (runStart[0] == rangeStart && runEnd[runCount - 1] == rangeStart) {
// The last run ends at the end of the range, and the first run
// starts at the beginning of the range. So they are actually
// part of the same run, except they wrap around the end of the
// range. To avoid adjacent runs, we need to merge them.
runStart[0] = runStart[runCount - 1];
runCount--;
}
}
return runCount;
}
private static int buildRunList(SparseBooleanArray cip, int rangeStart,
int rangeEnd, int[] runStart, int[] runEnd) {
int runCount = 0;
int i = findFirstSetIndex(cip, rangeStart, rangeEnd);
if (i == - 1) {
return 0;
}
int position = cip.keyAt(i);
int currentRunStart = position;
int currentRunEnd = currentRunStart + 1;
for (i++; i < cip.size() && (position = cip.keyAt(i)) < rangeEnd; i++) {
if (! cip.valueAt(i)) // not checked => not interesting
{
continue;
}
if (position == currentRunEnd) {
currentRunEnd++;
}
else {
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
currentRunStart = position;
currentRunEnd = position + 1;
}
}
if (currentRunEnd == rangeEnd) {
// rangeStart and rangeEnd are equivalent positions so to be
// consistent we translate them to the same integer value. That way
// we can check whether a run covers the entire range by just
// checking if the start equals the end position.
currentRunEnd = rangeStart;
}
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
if (runCount > 1) {
if (runStart[0] == rangeStart && runEnd[runCount - 1] == rangeStart) {
// The last run ends at the end of the range, and the first run
// starts at the beginning of the range. So they are actually
// part of the same run, except they wrap around the end of the
// range. To avoid adjacent runs, we need to merge them.
runStart[0] = runStart[runCount - 1];
runCount--;
}
}
return runCount;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
final SparseBooleanArray checked = mCurrentView.getCheckedItemPositions();
final ArrayList<DocumentInfo> docs = new ArrayList<>();
final int size = checked.size();
for (int i = 0; i < size; i++) {
if (checked.valueAt(i)) {
final Cursor cursor = mAdapter.getItem(checked.keyAt(i));
if(null != cursor) {
final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
docs.add(doc);
}
}
}
if(docs.isEmpty()){
return false;
}
final int id = item.getItemId();
switch (id) {
case R.id.menu_open:
BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
mode.finish();
return true;
case R.id.menu_share:
onShareDocuments(docs);
mode.finish();
return true;
case R.id.menu_copy:
moveDocument(docs, false);
mode.finish();
return true;
case R.id.menu_cut:
moveDocument(docs, true);
mode.finish();
return true;
case R.id.menu_delete:
deleteDocument(docs, id);
mode.finish();
return true;
case R.id.menu_stop:
stopDocument(docs, id);
mode.finish();
return true;
case R.id.menu_save:
case R.id.menu_compress:
new OperationTask(docs, id).execute();
mode.finish();
return true;
case R.id.menu_select_all:
int count = mAdapter.getCount();
for (int i = 0; i < count; i++) {
mCurrentView.setItemChecked(i, selectAll);
}
selectAll = !selectAll;
Bundle params = new Bundle();
params.putInt(FILE_COUNT, count);
AnalyticsManager.logEvent("select", params);
return true;
case R.id.menu_info:
infoDocument(docs.get(0));
mode.finish();
return true;
case R.id.menu_rename:
renameDocument(docs.get(0));
mode.finish();
return true;
default:
return false;
}
}
private static int buildRunList(@NonNull SparseBooleanArray cip, int rangeStart,
int rangeEnd, int[] runStart, int[] runEnd) {
int runCount = 0;
int i = findFirstSetIndex(cip, rangeStart, rangeEnd);
if (i == -1)
return 0;
int position = cip.keyAt(i);
int currentRunStart = position;
int currentRunEnd = currentRunStart + 1;
for (i++; i < cip.size() && (position = cip.keyAt(i)) < rangeEnd; i++) {
if (!cip.valueAt(i)) // not checked => not interesting
continue;
if (position == currentRunEnd) {
currentRunEnd++;
} else {
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
currentRunStart = position;
currentRunEnd = position + 1;
}
}
if (currentRunEnd == rangeEnd) {
// rangeStart and rangeEnd are equivalent positions so to be
// consistent we translate them to the same integer value. That way
// we can check whether a run covers the entire range by just
// checking if the start equals the end position.
currentRunEnd = rangeStart;
}
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
if (runCount > 1) {
if (runStart[0] == rangeStart && runEnd[runCount - 1] == rangeStart) {
// The last run ends at the end of the range, and the first run
// starts at the beginning of the range. So they are actually
// part of the same run, except they wrap around the end of the
// range. To avoid adjacent runs, we need to merge them.
runStart[0] = runStart[runCount - 1];
runCount--;
}
}
return runCount;
}
private static int buildRunList(SparseBooleanArray cip, int rangeStart,
int rangeEnd, int[] runStart, int[] runEnd) {
int runCount = 0;
int i = findFirstSetIndex(cip, rangeStart, rangeEnd);
if (i == -1)
return 0;
int position = cip.keyAt(i);
int currentRunStart = position;
int currentRunEnd = currentRunStart + 1;
for (i++; i < cip.size() && (position = cip.keyAt(i)) < rangeEnd; i++) {
if (!cip.valueAt(i)) // not checked => not interesting
continue;
if (position == currentRunEnd) {
currentRunEnd++;
} else {
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
currentRunStart = position;
currentRunEnd = position + 1;
}
}
if (currentRunEnd == rangeEnd) {
// rangeStart and rangeEnd are equivalent positions so to be
// consistent we translate them to the same integer value. That way
// we can check whether a run covers the entire range by just
// checking if the start equals the end position.
currentRunEnd = rangeStart;
}
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
if (runCount > 1) {
if (runStart[0] == rangeStart && runEnd[runCount - 1] == rangeStart) {
// The last run ends at the end of the range, and the first run
// starts at the beginning of the range. So they are actually
// part of the same run, except they wrap around the end of the
// range. To avoid adjacent runs, we need to merge them.
runStart[0] = runStart[runCount - 1];
runCount--;
}
}
return runCount;
}
private static int buildRunList(SparseBooleanArray cip, int rangeStart,
int rangeEnd, int[] runStart, int[] runEnd) {
int runCount = 0;
int i = findFirstSetIndex(cip, rangeStart, rangeEnd);
if (i == -1)
return 0;
int position = cip.keyAt(i);
int currentRunStart = position;
int currentRunEnd = currentRunStart + 1;
for (i++; i < cip.size() && (position = cip.keyAt(i)) < rangeEnd; i++) {
if (!cip.valueAt(i)) // not checked => not interesting
continue;
if (position == currentRunEnd) {
currentRunEnd++;
} else {
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
currentRunStart = position;
currentRunEnd = position + 1;
}
}
if (currentRunEnd == rangeEnd) {
// rangeStart and rangeEnd are equivalent positions so to be
// consistent we translate them to the same integer value. That way
// we can check whether a run covers the entire range by just
// checking if the start equals the end position.
currentRunEnd = rangeStart;
}
runStart[runCount] = currentRunStart;
runEnd[runCount] = currentRunEnd;
runCount++;
if (runCount > 1) {
if (runStart[0] == rangeStart && runEnd[runCount - 1] == rangeStart) {
// The last run ends at the end of the range, and the first run
// starts at the beginning of the range. So they are actually
// part of the same run, except they wrap around the end of the
// range. To avoid adjacent runs, we need to merge them.
runStart[0] = runStart[runCount - 1];
runCount--;
}
}
return runCount;
}