Example usage for android.support.v4.util SparseArrayCompat put

List of usage examples for android.support.v4.util SparseArrayCompat put

Introduction

In this page you can find the example usage for android.support.v4.util SparseArrayCompat put.

Prototype

public void put(int i, E e) 

Source Link

Usage

From source file:com.facebook.litho.ComponentHostUtils.java

/**
 * Takes the item at position index from items and puts it into scrapItems. If no such item exists
 * the invocation of this method will have no effect.
 *//*  w  w w  .  java 2 s. c o m*/
static <T> void scrapItemAt(int index, SparseArrayCompat<T> items, SparseArrayCompat<T> scrapItems) {
    final T value = items.get(index);
    if (value != null) {
        scrapItems.put(index, value);
    }
}

From source file:Main.java

public static <T extends View> T get(View view, int id) {
    SparseArrayCompat<View> viewHolder = (SparseArrayCompat<View>) view.getTag();
    if (viewHolder == null) {
        viewHolder = new SparseArrayCompat<>();
        view.setTag(viewHolder);//w w w  . ja v  a2  s.  co  m
    }
    View childView = viewHolder.get(id);
    if (childView == null) {
        childView = view.findViewById(id);
        viewHolder.put(id, childView);
    }
    return (T) childView;
}

From source file:com.brotherpowers.cameraview.AspectRatio.java

/**
 * Returns an instance of {@link AspectRatio} specified by {@code x} and {@code y} values.
 * The values {@code x} and {@code} will be reduced by their greatest common divider.
 *
 * @param x The width//from  w  w  w .  j a  v a  2s. c  o  m
 * @param y The height
 * @return An instance of {@link AspectRatio}
 */
public static AspectRatio of(int x, int y) {
    int gcd = gcd(x, y);
    x /= gcd;
    y /= gcd;
    SparseArrayCompat<AspectRatio> arrayX = sCache.get(x);
    if (arrayX == null) {
        AspectRatio ratio = new AspectRatio(x, y);
        arrayX = new SparseArrayCompat<>();
        arrayX.put(y, ratio);
        sCache.put(x, arrayX);
        return ratio;
    } else {
        AspectRatio ratio = arrayX.get(y);
        if (ratio == null) {
            ratio = new AspectRatio(x, y);
            arrayX.put(y, ratio);
        }
        return ratio;
    }
}

From source file:com.facebook.litho.ComponentHostUtils.java

/**
 * Moves an item from oldIndex to newIndex. The item is taken from scrapitems if an item exists
 * in scrapItems at oldPosition. Otherwise the item is taken from items. This assumes that there
 * is no item at newIndex for the items array. If that's the case
 * {@link ComponentHostUtils#scrapItemAt(int, SparseArrayCompat, SparseArrayCompat)}
 * has to be called before invoking this.
 *//*w ww .  j a  v  a 2s  .c o m*/
static <T> void moveItem(int oldIndex, int newIndex, SparseArrayCompat<T> items,
        SparseArrayCompat<T> scrapItems) {
    T itemToMove;

    if (existsScrapItemAt(oldIndex, scrapItems)) {
        // Before moving the item from items we need to check whether an old item has been put in
        // the scrapItems array. If there is an item at oldIndex there, it means that in
        // items at position oldIndex there's now something else and the correct item to move to
        // newIndex is instead in the scrapItems SparseArray.
        itemToMove = scrapItems.get(oldIndex);
        scrapItems.remove(oldIndex);
    } else {
        itemToMove = items.get(oldIndex);
        items.remove(oldIndex);
    }

    items.put(newIndex, itemToMove);
}

From source file:codetoanalyze.java.checkers.ContainerWrapper.java

public void addToSparseArrayCompatBad(SparseArrayCompat sparseArray) {
    sparseArray.put(0, new Object());
}

From source file:codetoanalyze.java.checkers.ContainerWrapper.java

void addToSparseArrayCompatOk() {
    SparseArrayCompat sparseArray = new SparseArrayCompat();
    sparseArray.put(0, new Object());
}

From source file:net.xisberto.work_schedule.database.Database.java

public SparseArrayCompat<Period> listPeriodsFromDay(Calendar day) {
    SimpleDateFormat dayFormat = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
    log("listing periods for " + dayFormat.format(day.getTime()));
    Cursor cursor = db.query(TablePeriod.TABLE_NAME, TablePeriod.COLUMNS,
            TablePeriod.COLUMN_TIME + " LIKE '" + dayFormat.format(day.getTime()) + "%'", null, null, null,
            TablePeriod.COLUMN_PREF_ID);

    SparseArrayCompat<Period> result = new SparseArrayCompat<Period>(8);

    if (cursor != null && cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            Period p = periodFromCursor(cursor);
            result.put(p.getId(), p);
        }/*  ww w . j  a v a2 s  .  c o  m*/
    }

    return result;
}

From source file:com.granita.tasks.groupings.BaseTaskViewDescriptor.java

protected <T extends View> T getView(View view, int viewId) {
    SparseArrayCompat<View> viewHolder = (SparseArrayCompat<View>) view.getTag();
    if (viewHolder == null) {
        viewHolder = new SparseArrayCompat<View>();
        view.setTag(viewHolder);//  w  w  w  .ja  v a 2s.c om
    }
    View res = viewHolder.get(viewId);
    if (res == null) {
        res = view.findViewById(viewId);
        viewHolder.put(viewId, res);
    }
    return (T) res;
}

From source file:com.android.deskclock.timer.FragmentStatePagerAdapter2.java

@Override
public void notifyDataSetChanged() {
    // update positions in mFragments
    SparseArrayCompat<Fragment> newFragments = new SparseArrayCompat<Fragment>(mFragments.size());
    for (int i = 0; i < mFragments.size(); i++) {
        final int oldPos = mFragments.keyAt(i);
        final Fragment f = mFragments.valueAt(i);
        final int newPos = getItemPosition(f);

        if (newPos != POSITION_NONE) {
            final int pos = (newPos >= 0) ? newPos : oldPos;
            newFragments.put(pos, f);
        }//  w w w  .j a va  2 s.  c  om
    }
    mFragments = newFragments;

    super.notifyDataSetChanged();
}

From source file:com.mvcoding.financius.data.paging.PageLoader.java

@NonNull
public Observable<PageResult<T>> load(@NonNull DataConverter<T> dataConverter,
        @NonNull DatabaseQuery databaseQuery, @NonNull Observable<Page> pageObservable) {
    final SparseArrayCompat<T> cache = new SparseArrayCompat<>();
    final Observable<Cursor> cursorObservable = database.load(databaseQuery).doOnNext(cursor -> cache.clear());

    return Observable.combineLatest(pageObservable, cursorObservable, (page, cursor) -> {
        final List<T> items = new ArrayList<>();
        for (int i = page.getStart(), size = Math.min(cursor.getCount(),
                page.getStart() + page.getSize()); i < size; i++) {
            T item = cache.get(i);/*from w w  w.  ja va2 s . co m*/
            if (item == null) {
                cursor.moveToPosition(i);
                item = dataConverter.from(cursor);
                cache.put(i, item);
            }
            items.add(item);
        }

        return new PageResult<>(page, items, cache.size() == items.size());
    });
}