Java Code Examples for android.util.SparseBooleanArray#keyAt()

The following examples show how to use android.util.SparseBooleanArray#keyAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SelectPopupDialog.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private static 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;
}
 
Example 2
Source File: TableLayout.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Applies the columns collapse status to a new row added to this
 * table. This method is invoked by PassThroughHierarchyChangeListener
 * upon child insertion.</p>
 *
 * <p>This method only applies to {@link android.widget.TableRow}
 * instances.</p>
 *
 * @param child the newly added child
 */
private void trackCollapsedColumns(View child) {
    if (child instanceof TableRow) {
        final TableRow row = (TableRow) child;
        final SparseBooleanArray collapsedColumns = mCollapsedColumns;
        final int count = collapsedColumns.size();
        for (int i = 0; i < count; i++) {
            int columnIndex = collapsedColumns.keyAt(i);
            boolean isCollapsed = collapsedColumns.valueAt(i);
            // the collapse status is set only when the column should be
            // collapsed; otherwise, this might affect the default
            // visibility of the row's children
            if (isCollapsed) {
                row.setColumnCollapsed(columnIndex, isCollapsed);
            }
        }
    }
}
 
Example 3
Source File: DragSortListView.java    From biermacht with Apache License 2.0 6 votes vote down vote up
/**
 * Use this when an item has been deleted, to move the check state of all following items up one
 * step. If you have a choiceMode which is not none, this method must be called when the order of
 * items changes in an underlying adapter which does not have stable IDs (see {@link
 * android.widget.ListAdapter#hasStableIds()}). This is because without IDs, the ListView has no
 * way of knowing which items have moved where, and cannot update the check state accordingly.
 * <p/>
 * See also further comments on {@link #moveCheckState(int, int)}.
 *
 * @param position
 */
public void removeCheckState(int position) {
  SparseBooleanArray cip = getCheckedItemPositions();

  if (cip.size() == 0) {
    return;
  }
  int[] runStart = new int[cip.size()];
  int[] runEnd = new int[cip.size()];
  int rangeStart = position;
  int rangeEnd = cip.keyAt(cip.size() - 1) + 1;
  int runCount = buildRunList(cip, rangeStart, rangeEnd, runStart, runEnd);
  for (int i = 0; i != runCount; i++) {
    if (! (runStart[i] == position || (runEnd[i] < runStart[i] && runEnd[i] > position))) {
      // Only set a new check mark in front of this run if it does
      // not contain the deleted position. If it does, we only need
      // to make it one check mark shorter at the end.
      setItemChecked(rotate(runStart[i], - 1, rangeStart, rangeEnd), true);
    }
    setItemChecked(rotate(runEnd[i], - 1, rangeStart, rangeEnd), false);
  }
}
 
Example 4
Source File: DragSortListView.java    From simpletask-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Use this when an item has been deleted, to move the check state of all
 * following items up one step. If you have a choiceMode which is not none,
 * this method must be called when the order of items changes in an
 * underlying adapter which does not have stable IDs (see
 * {@link ListAdapter#hasStableIds()}). This is because without IDs, the
 * ListView has no way of knowing which items have moved where, and cannot
 * updateCache the check state accordingly.
 * 
 * See also further comments on {@link #moveCheckState(int, int)}.
 * 
 * @param position
 */
public void removeCheckState(int position) {
    SparseBooleanArray cip = getCheckedItemPositions();

    if (cip.size() == 0)
        return;
    int[] runStart = new int[cip.size()];
    int[] runEnd = new int[cip.size()];
    int rangeEnd = cip.keyAt(cip.size() - 1) + 1;
    int runCount = buildRunList(cip, position, rangeEnd, runStart, runEnd);
    for (int i = 0; i != runCount; i++) {
        if (!(runStart[i] == position || (runEnd[i] < runStart[i] && runEnd[i] > position))) {
            // Only set a new check mark in front of this run if it does
            // not contain the deleted position. If it does, we only need
            // to make it one check mark shorter at the end.
            setItemChecked(rotate(runStart[i], -1, position, rangeEnd), true);
        }
        setItemChecked(rotate(runEnd[i], -1, position, rangeEnd), false);
    }
}
 
Example 5
Source File: SelectPopupDialog.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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;
}
 
Example 6
Source File: DragSortListView.java    From google-authenticator-android with Apache License 2.0 5 votes vote down vote up
private static int insertionIndexForKey(SparseBooleanArray sba, int key) {
    int low = 0;
    int high = sba.size();
    while (high - low > 0) {
        int middle = (low + high) >> 1;
        if (sba.keyAt(middle) < key)
            low = middle + 1;
        else
            high = middle;
    }
    return low;
}
 
Example 7
Source File: DragSortListView.java    From Field-Book with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: DragSortListView.java    From google-authenticator-android with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: LovelyChoiceDialog.java    From LovelyDialog with Apache License 2.0 5 votes vote down vote up
@Override
void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (isMultiChoiceList()) {
        ListAdapter adapter = choicesList.getAdapter();
        boolean[] checkedStates = new boolean[adapter.getCount()];
        SparseBooleanArray checkedPositions = choicesList.getCheckedItemPositions();
        for (int i = 0; i < checkedPositions.size(); i++) {
            if (checkedPositions.valueAt(i)) {
                checkedStates[checkedPositions.keyAt(i)] = true;
            }
        }
        outState.putBooleanArray(KEY_ITEM_CHECKED_STATES, checkedStates);
    }
}
 
Example 10
Source File: PlayMultipleDialogFragment.java    From mytracks with Apache License 2.0 5 votes vote down vote up
private long[] getChecked(SparseBooleanArray array) {
  ArrayList<Long> checked = new ArrayList<Long>();
  for (int i = 0; i < array.size(); i++) {
    int key = array.keyAt(i);
    if (array.get(key)) {
      checked.add(trackIds[key]);
    }
  }
  long[] result = new long[checked.size()];
  for (int i = 0; i < checked.size(); i++) {
    result[i] = checked.get(i).longValue();
  }
  return result;
}
 
Example 11
Source File: WeekAdapter.java    From TimeTable with GNU General Public License v3.0 5 votes vote down vote up
private void hidePopUpMenu(ViewHolder holder) {
    SparseBooleanArray checkedItems = mListView.getCheckedItemPositions();
    if (checkedItems.size() > 0) {
        for (int i = 0; i < checkedItems.size(); i++) {
            int key = checkedItems.keyAt(i);
            if (checkedItems.get(key)) {
                holder.popup.setVisibility(View.INVISIBLE);
                }
        }
    } else {
        holder.popup.setVisibility(View.VISIBLE);
    }
}
 
Example 12
Source File: NotesAdapter.java    From TimeTable with GNU General Public License v3.0 5 votes vote down vote up
private void hidePopUpMenu(ViewHolder holder) {
    SparseBooleanArray checkedItems = mListView.getCheckedItemPositions();
    if (checkedItems.size() > 0) {
        for (int i = 0; i < checkedItems.size(); i++) {
            int key = checkedItems.keyAt(i);
            if (checkedItems.get(key)) {
                holder.popup.setVisibility(View.INVISIBLE);
                }
        }
    } else {
        holder.popup.setVisibility(View.VISIBLE);
    }
}
 
Example 13
Source File: AppStateTracker.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void cleanUpArrayForUser(SparseBooleanArray array, int removedUserId) {
    for (int i = array.size() - 1; i >= 0; i--) {
        final int uid = array.keyAt(i);
        final int userId = UserHandle.getUserId(uid);

        if (userId == removedUserId) {
            array.removeAt(i);
        }
    }
}
 
Example 14
Source File: ExamsAdapter.java    From TimeTable with GNU General Public License v3.0 5 votes vote down vote up
private void hidePopUpMenu(ViewHolder holder) {
    SparseBooleanArray checkedItems = mListView.getCheckedItemPositions();
    if (checkedItems.size() > 0) {
        for (int i = 0; i < checkedItems.size(); i++) {
            int key = checkedItems.keyAt(i);
            if (checkedItems.get(key)) {
                holder.popup.setVisibility(View.INVISIBLE);
            }
        }
    } else {
        holder.popup.setVisibility(View.VISIBLE);
    }
}
 
Example 15
Source File: DragSortListView.java    From simpletask-android with GNU General Public License v3.0 5 votes vote down vote up
private static int findFirstSetIndex(@NonNull 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;
}
 
Example 16
Source File: MainActivity.java    From android-tutorials-custom-selectable-listview with Apache License 2.0 5 votes vote down vote up
/**
 * Show a message giving the selected item captions
 */
private void showSelectedItems() {
	final StringBuffer sb = new StringBuffer("Selection: ");

	// Get an array that tells us for each position whether the item is
	// checked or not
	// --
	final SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
	if (checkedItems == null) {
		Toast.makeText(this, "No selection info available", Toast.LENGTH_LONG).show();
		return;
	}

	// For each element in the status array
	// --
	boolean isFirstSelected = true;
	final int checkedItemsCount = checkedItems.size();
	for (int i = 0; i < checkedItemsCount; ++i) {
		// This tells us the item position we are looking at
		// --
		final int position = checkedItems.keyAt(i);

		// This tells us the item status at the above position
		// --
		final boolean isChecked = checkedItems.valueAt(i);

		if (isChecked) {
			if (!isFirstSelected) {
				sb.append(", ");
			}
			sb.append(data.get(position).getCaption());
			isFirstSelected = false;
		}
	}

	// Show a message with the countries that are selected
	// --
	Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
}
 
Example 17
Source File: DragSortListView.java    From android-kernel-tweaker with GNU General Public License v3.0 5 votes vote down vote up
private static int insertionIndexForKey(SparseBooleanArray sba, int key) {
    int low = 0;
    int high = sba.size();
    while (high - low > 0) {
        int middle = (low + high) >> 1;
        if (sba.keyAt(middle) < key)
            low = middle + 1;
        else
            high = middle;
    }
    return low;
}
 
Example 18
Source File: DragSortListView.java    From simpletask-android with GNU General Public License v3.0 5 votes vote down vote up
private static int insertionIndexForKey(@NonNull SparseBooleanArray sba, int key) {
    int low = 0;
    int high = sba.size();
    while (high - low > 0) {
        int middle = (low + high) >> 1;
        if (sba.keyAt(middle) < key)
            low = middle + 1;
        else
            high = middle;
    }
    return low;
}
 
Example 19
Source File: ServerListActivity.java    From padland with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 20
Source File: BlobDescriptorListFragment.java    From aard2-android with GNU General Public License v3.0 5 votes vote down vote up
protected void deleteSelectedItems() {
    SparseBooleanArray checkedItems = getListView().getCheckedItemPositions();
    for (int i = checkedItems.size() - 1; i > -1; --i) {
        int position = checkedItems.keyAt(i);
        boolean checked = checkedItems.get(position);
        if (checked) {
            getDescriptorList().remove(position);
        }
     }
}