Example usage for android.util SparseArray get

List of usage examples for android.util SparseArray get

Introduction

In this page you can find the example usage for android.util SparseArray get.

Prototype

public E get(int key) 

Source Link

Document

Gets the Object mapped from the specified key, or null if no such mapping has been made.

Usage

From source file:Main.java

public static void recycleBitmaps(SparseArray<Bitmap> bitmapSparseArray) {
    for (int i = 0; i < bitmapSparseArray.size(); i++) {
        Bitmap mBitmap = bitmapSparseArray.get(i);
        if (mBitmap != null) {
            mBitmap.recycle();/*from   w w  w.  ja v a 2 s.  c  o m*/
            mBitmap = null;
        }
    }
}

From source file:Main.java

public static float sumCompartmentsValues(SparseArray<ArrayList<Float>> compartments) {
    float sum = 0L;
    for (int i = 0; i < compartments.size(); i++) {
        for (int j = 0; j < compartments.get(i).size(); j++) {
            sum += compartments.get(i).get(j);
        }//  w w w. j  ava2  s.c om
    }
    return sum;
}

From source file:Main.java

public static <T> void deleteInArray(SparseArray<SparseArray<T>> array, int firstKey, int secondKey) {
    if (array == null)
        return;// w  w w  . j av a2  s .c  om

    SparseArray<T> ts = array.get(firstKey);
    if (ts == null)
        return;

    synchronized (array) {
        ts.delete(secondKey);

        if (ts.size() == 0) {
            array.delete(firstKey);
        }
    }
}

From source file:Main.java

public static <T> void addInArray(SparseArray<SparseArray<T>> array, int firstKey, int secondKey, T value) {
    if (array == null)
        return;/*  www.j  a  va 2 s .c  o m*/

    SparseArray<T> ts = array.get(firstKey);
    if (ts == null) {
        ts = new SparseArray<T>();

        synchronized (array) {
            array.put(firstKey, ts);
        }
    }

    synchronized (array) {
        ts.put(secondKey, value);
    }
}

From source file:Main.java

public static SparseArray<Float> compartmentsMaxValues(SparseArray<ArrayList<Float>> compartments) {
    SparseArray<Float> maxValues = new SparseArray<>();
    for (int i = 0; i < compartments.size(); i++) {
        maxValues.put(i, Collections.max(compartments.get(i)));
    }//from   ww w  .j  a v a  2 s.co  m
    return maxValues;
}

From source file:Main.java

/**
 * Converts the sparse array into {@link java.util.List}.
 *
 * @param array sparse array of some type
 * @param <T> type of object in sparse array
 * @return list of object of specific type
 *//*from  w ww  . j  av a2 s.  co  m*/
public static <T> List<T> sparseArrayAsList(final SparseArray<T> array) {
    final List<T> values = new ArrayList<>();

    for (int i = 0; i < array.size(); i++) {
        final int key = array.keyAt(i);

        values.add(array.get(key));
    }

    return values;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T extends View> T obtainView(View convertView, int id) {
    SparseArray<View> holder = (SparseArray<View>) convertView.getTag();
    if (holder == null) {
        holder = new SparseArray<View>();
        convertView.setTag(holder);//from w w  w . j  a  v a 2  s  .co m
    }
    View childView = holder.get(id);
    if (childView == null) {
        childView = convertView.findViewById(id);
        holder.put(id, childView);
    }
    return (T) childView;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T extends View> T getViewByHolder(View view, int id) {
    SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
    if (viewHolder == null) {
        viewHolder = new SparseArray<View>();
        view.setTag(viewHolder);/* www  .j av  a 2  s  .c om*/
    }
    View childView = viewHolder.get(id);
    if (childView == null) {
        childView = view.findViewById(id);
        viewHolder.put(id, childView);
    }
    return (T) childView;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T extends View> T get(View view, int id) {
    SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
    if (viewHolder == null) {
        viewHolder = new SparseArray<View>();
        view.setTag(viewHolder);//w w w  .  j a va 2 s  .com
    }
    View childView = viewHolder.get(id);
    if (childView == null) {
        childView = view.findViewById(id);
        viewHolder.put(id, childView);
    }
    return (T) childView;
}

From source file:Main.java

public static <T extends View> T hold(View view, int id) {
    SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();

    if (viewHolder == null) {
        viewHolder = new SparseArray<View>();
        view.setTag(viewHolder);//from w w  w . j  a  v  a2s.c om
    }

    View childView = viewHolder.get(id);

    if (childView == null) {
        childView = view.findViewById(id);
        viewHolder.put(id, childView);
    }

    return (T) childView;
}