Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 34783aac authored by Gilles Debunne's avatar Gilles Debunne
Browse files

Fixes for HeaderViewListAdapter.

Null header and footer, as used by CTS tests, were no longer supported.
Added a static empty list to avoid repetitive null tests in that case.
Fixed getItem/getItemId/getView to handle corner cases.
Changed ListAdapter isEnabled documentation for invalid positions.

http://b/issue?id=2527753
Change-Id: I55e5bf21cb0673d906caa7c669987a6ae869d90f
parent 3e8b72ac
Loading
Loading
Loading
Loading
+44 −30
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import java.util.ArrayList;
 * associated data objects.
 *<p>This is intended as a base class; you will probably not need to
 * use this class directly in your own code.
 *
 */
public class HeaderViewListAdapter implements WrapperListAdapter, Filterable {

@@ -130,43 +129,53 @@ public class HeaderViewListAdapter implements WrapperListAdapter, Filterable {
    }

    public boolean isEnabled(int position) {
        // Header (negative positions will throw an ArrayIndexOutOfBoundsException)
        int numHeaders = getHeadersCount();
        if (mAdapter != null && position >= numHeaders) {
            int adjPosition = position - numHeaders;
            int adapterCount = mAdapter.getCount();
            if (adjPosition >= adapterCount) {
                return mFooterViewInfos.get(adjPosition - adapterCount).isSelectable;
            } else {
        if (position < numHeaders) {
            return mHeaderViewInfos.get(position).isSelectable;
        }

        // Adapter
        final int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (mAdapter != null) {
            adapterCount = mAdapter.getCount();
            if (adjPosition < adapterCount) {
                return mAdapter.isEnabled(adjPosition);
            }
        } else if (position < numHeaders) {
            return mHeaderViewInfos.get(position).isSelectable;
        }
        return true;

        // Footer (off-limits positions will throw an ArrayIndexOutOfBoundsException)
        return mFooterViewInfos.get(adjPosition - adapterCount).isSelectable;
    }

    public Object getItem(int position) {
        // Header (negative positions will throw an ArrayIndexOutOfBoundsException)
        int numHeaders = getHeadersCount();
        if (mAdapter != null && position >= numHeaders) {
            int adjPosition = position - numHeaders;
            int adapterCount = mAdapter.getCount();
            if (adjPosition >= adapterCount) {
                return mFooterViewInfos.get(adjPosition - adapterCount).data;
            } else {
        if (position < numHeaders) {
            return mHeaderViewInfos.get(position).data;
        }

        // Adapter
        final int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (mAdapter != null) {
            adapterCount = mAdapter.getCount();
            if (adjPosition < adapterCount) {
                return mAdapter.getItem(adjPosition);
            }
        } else if (position < numHeaders) {
            return mHeaderViewInfos.get(position).data;
        }
        return null;

        // Footer (off-limits positions will throw an ArrayIndexOutOfBoundsException)
        return mFooterViewInfos.get(adjPosition - adapterCount).data;
    }

    public long getItemId(int position) {
        int numHeaders = getHeadersCount();
        if (mAdapter != null && position >= numHeaders) {
            int adjPosition = position - numHeaders;
            int adapterCnt = mAdapter.getCount();
            if (adjPosition < adapterCnt) {
            int adapterCount = mAdapter.getCount();
            if (adjPosition < adapterCount) {
                return mAdapter.getItemId(adjPosition);
            }
        }
@@ -181,19 +190,24 @@ public class HeaderViewListAdapter implements WrapperListAdapter, Filterable {
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // Header (negative positions will throw an ArrayIndexOutOfBoundsException)
        int numHeaders = getHeadersCount();
        if (mAdapter != null && position >= numHeaders) {
            int adjPosition = position - numHeaders;
            int adapterCount = mAdapter.getCount();
            if (adjPosition >= adapterCount) {
                return mFooterViewInfos.get(adjPosition - adapterCount).view;
            } else {
        if (position < numHeaders) {
            return mHeaderViewInfos.get(position).view;
        }

        // Adapter
        final int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (mAdapter != null) {
            adapterCount = mAdapter.getCount();
            if (adjPosition < adapterCount) {
                return mAdapter.getView(adjPosition, convertView, parent);
            }
        } else if (position < numHeaders) {
            return mHeaderViewInfos.get(position).view;
        }
        return null;

        // Footer (off-limits positions will throw an ArrayIndexOutOfBoundsException)
        return mFooterViewInfos.get(adjPosition - adapterCount).view;
    }

    public int getItemViewType(int position) {
+3 −0
Original line number Diff line number Diff line
@@ -37,6 +37,9 @@ public interface ListAdapter extends Adapter {
     * Returns true if the item at the specified position is not a separator.
     * (A separator is a non-selectable, non-clickable item).
     * 
     * The result is unspecified if position is invalid. An {@link ArrayIndexOutOfBoundsException}
     * should be thrown in that case for fast failure.
     *
     * @param position Index of the item
     * @return True if the item is not a separator
     */