/** * Computes the widest view in an adapter, best used when you need to wrap_content on a ListView, please be careful * and don't use it on an adapter that is extremely numerous in items or it will take a long time. * * @param context Some context * @param adapter The adapter to process * @return The pixel width of the widest View */ public static int getWidestView(Context context, Adapter adapter) { int maxWidth = 0; View view = null; FrameLayout fakeParent = new FrameLayout(context); for (int i=0, count=adapter.getCount(); i<count; i++) { view = adapter.getView(i, view, fakeParent); view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); int width = view.getMeasuredWidth(); if (width > maxWidth) { maxWidth = width; } } return maxWidth; }
public S hasCount(int count) { isNotNull(); int actualCount = actual.getCount(); assertThat(actualCount) // .overridingErrorMessage("Expected count <%s> but was <%s>.", count, actualCount) // .isEqualTo(count); return myself; }
public S hasItem(Object expected, int index) { isNotNull(); assertThat(actual.getCount()) .overridingErrorMessage("Index %s is out of bounds. The adapter holds %s items.", index, actual.getCount()) .isGreaterThan(index); final Object actualItem = actual.getItem(index); assertThat(actualItem) .overridingErrorMessage("Expected item at index %s to be <%s> but was <%s>.", index, expected, actualItem) .isEqualTo(expected); return myself; }
/** * @return the items that are available to be clicked on */ public CharSequence[] getItems() { Adapter adapter = getShadowAlertController().getAdapter(); int count = adapter.getCount(); CharSequence[] items = new CharSequence[count]; for (int i = 0; i < items.length; i++) { items[i] = (CharSequence) adapter.getItem(i); } return items; }
@Override public void check(View view, NoMatchingViewException noViewFoundException) { Assert.assertTrue(view instanceof AdapterView); Assert.assertEquals(count, ((AdapterView)view).getAdapter().getCount()); } }
public S doesNotHaveItem(Object notExpected, int index) { isNotNull(); assertThat(actual.getCount()) .overridingErrorMessage("Index %s is out of bounds. The adapter holds %s items.", index, actual.getCount()) .isGreaterThan(index); final Object actualItem = actual.getItem(index); assertThat(actualItem) .overridingErrorMessage("Expected item at index %s not to be <%s> but it was.", index, notExpected, actualItem) .isNotEqualTo(notExpected); return myself; } }
@Override public void check(View view, NoMatchingViewException noViewFoundException) { Assert.assertTrue(view instanceof AdapterView); Assert.assertEquals(count, ((AdapterView)view).getAdapter().getCount()); } }}
/** * @return The data corresponding to the currently selected item, or * null if there is nothing selected. */ public Object getSelectedItem() { T adapter = getAdapter(); int selection = getSelectedItemPosition(); if (adapter != null && adapter.getCount() > 0 && selection >= 0) { return adapter.getItem(selection); } else { return null; } }
/** * Remember enough information to restore the screen state when the data has * changed. * */ void rememberSyncState() { if (getChildCount() > 0) { mNeedSync = true; mSyncHeight = mLayoutHeight; // Sync the based on the offset of the first view View v = getChildAt(0); T adapter = getAdapter(); if (mFirstPosition >= 0 && mFirstPosition < adapter.getCount()) { mSyncRowId = adapter.getItemId(mFirstPosition); } else { mSyncRowId = NO_ID; } mSyncPosition = mFirstPosition; if (v != null) { mSpecificTop = v.getTop(); } mSyncMode = SYNC_FIRST_POSITION; } } }
@Test public void testBuilderWithAdapter() throws Exception { List<Integer> list = new ArrayList<>(); list.add(99); list.add(88); list.add(77); ArrayAdapter<Integer> adapter = new ArrayAdapter<>(context, R.layout.main, R.id.title, list); AlertDialog.Builder builder = new AlertDialog.Builder(application); builder.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); assertTrue(alert.isShowing()); ShadowAlertDialog shadowAlertDialog = shadowOf(alert); assertThat(shadowAlertDialog.getAdapter().getCount()).isEqualTo(3); assertThat(shadowAlertDialog.getAdapter().getItem(0)).isEqualTo(99); }
public void setSelection(int position) { if (position < mAdapter.getCount()) { this.nextAdapterCard = position; removeAllViews(); requestLayout(); } }
private void addNextCard() { if (nextAdapterCard < mAdapter.getCount()) { // TODO: Make view recycling work // TODO: Instead of removing the view from here and adding it again when it's swiped // ... don't remove and add to this instance: don't call removeView & addView in sequence. View newBottomChild = mAdapter.getView(nextAdapterCard, null/*lastRemovedView*/, this); if (hardwareAccelerationEnabled) { //set backed by an off-screen buffer newBottomChild.setLayerType(View.LAYER_TYPE_HARDWARE, null); } //set the initial Y value so card appears from under the deck //newBottomChild.setY(paddingTop); addAndMeasureChild(newBottomChild); nextAdapterCard++; } setupTopCard(); }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); // if we don't have an adapter, we don't need to do anything if (mAdapter == null || mAdapter.getCount() == 0) { nextAdapterCard = 0; removeAllViewsInLayout(); return; } //pull in views from the adapter at the position the top of the deck is set to //stop when you get to for cards or the end of the adapter int childCount = getChildCount(); for (int i = childCount; i < NUMBER_OF_CARDS; ++i) { addNextCard(); } for (int i = 0; i < getChildCount(); ++i) { positionItem(i); } //position the new children we just added and set up the top card with a listener etc }
final int dataPosition = i - getHeaderViewsCount(); final int childPosition = i - getFirstVisiblePosition(); if (dataPosition >= 0 && dataPosition < mAdapter.getCount() && getChildAt(childPosition) != null) { Log.v(TAG, "Refreshing view (data=" + dataPosition + ",child=" + childPosition + ")");
final int adapterCount = mAdapter.getCount(); if (adapterCount == 0) {
static final int getAdapterPositionById(final Adapter adapter, final long id) throws NoSuchElementException { final int count = adapter.getCount(); for (int pos = 0; pos < count; pos++) { if (id == adapter.getItemId(pos)) { return pos; } } throw new NoSuchElementException(); }
public List<User> retrieveAllItems(Spinner theSpinner) { Adapter adapter = theSpinner.getAdapter(); int n = adapter.getCount(); List<User> users = new ArrayList<User>(n); for (int i = 0; i < n; i++) { User user = (User) adapter.getItem(i); users.add(user); } return users; }
private boolean isScrollableForAccessibility() { T adapter = getAdapter(); if (adapter != null) { final int itemCount = adapter.getCount(); return itemCount > 0 && (getFirstVisiblePosition() > 0 || getLastVisiblePosition() < itemCount - 1); } return false; }
/** * @return the items that are available to be clicked on */ public CharSequence[] getItems() { Adapter adapter = getShadowAlertController().getAdapter(); int count = adapter.getCount(); CharSequence[] items = new CharSequence[count]; for (int i = 0; i < items.length; i++) { items[i] = (CharSequence) adapter.getItem(i); } return items; }
@Override public void setFocusable(boolean focusable) { final T adapter = getAdapter(); final boolean empty = adapter == null || adapter.getCount() == 0; mDesiredFocusableState = focusable; if (!focusable) { mDesiredFocusableInTouchModeState = false; } super.setFocusable(focusable && (!empty || isInFilterMode())); }