Java Code Examples for android.widget.ListAdapter#getItemId()

The following examples show how to use android.widget.ListAdapter#getItemId() . 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: DynamicListView.java    From android-open-project-demo with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the view in the list corresponding to itemId
 */
private View getViewForId(long itemId) {
    int firstVisiblePosition = getFirstVisiblePosition();
    ListAdapter adapter = getAdapter();
    if (!adapter.hasStableIds()) {
        throw new IllegalStateException("Adapter doesn't have stable ids! Make sure your adapter has stable ids, and override hasStableIds() to return true.");
    }

    for (int i = 0; i < getChildCount(); i++) {
        View v = getChildAt(i);
        int position = firstVisiblePosition + i;
        long id = adapter.getItemId(position);
        if (id == itemId) {
            return v;
        }
    }
    return null;
}
 
Example 2
Source File: DynamicListView.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the view in the list corresponding to itemId
 */
private View getViewForId(long itemId) {
    int firstVisiblePosition = getFirstVisiblePosition();
    ListAdapter adapter = getAdapter();
    if (!adapter.hasStableIds()) {
        throw new IllegalStateException("Adapter doesn't have stable ids! Make sure your adapter has stable ids, and override hasStableIds() to return true.");
    }

    for (int i = 0; i < getChildCount(); i++) {
        View v = getChildAt(i);
        int position = firstVisiblePosition + i;
        long id = adapter.getItemId(position);
        if (id == itemId) {
            return v;
        }
    }
    return null;
}
 
Example 3
Source File: ExtendableListView.java    From PullToRefreshLibrary with Apache License 2.0 6 votes vote down vote up
/**
 * Remember enough information to restore the screen state when the data has
 * changed.
 */
void rememberSyncState() {
	if (getChildCount() > 0) {
		mNeedSync = true;
		mSyncHeight = getHeight();
		// Sync the based on the offset of the first view
		View v = getChildAt(0);
		ListAdapter adapter = getAdapter();
		if (mFirstPosition >= 0 && mFirstPosition < adapter.getCount()) {
			mSyncRowId = adapter.getItemId(mFirstPosition);
		} else {
			mSyncRowId = NO_ID;
		}
		if (v != null) {
			mSpecificTop = v.getTop();
		}
		mSyncPosition = mFirstPosition;
	}
}
 
Example 4
Source File: DragAndDropHandler.java    From ListViewAnimations with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the {@code View} in the list corresponding to itemId.
 *
 * @return the {@code View}, or {@code null} if not found.
 */
@Nullable
private View getViewForId(final long itemId) {
    ListAdapter adapter = mAdapter;
    if (itemId == INVALID_ID || adapter == null) {
        return null;
    }

    int firstVisiblePosition = mWrapper.getFirstVisiblePosition();

    View result = null;
    for (int i = 0; i < mWrapper.getChildCount() && result == null; i++) {
        int position = firstVisiblePosition + i;
        if (position - mWrapper.getHeaderViewsCount() >= 0) {
            long id = adapter.getItemId(position - mWrapper.getHeaderViewsCount());
            if (id == itemId) {
                result = mWrapper.getChildAt(i);
            }
        }
    }
    return result;
}
 
Example 5
Source File: DynamicListView.java    From ALLGO with Apache License 2.0 6 votes vote down vote up
/** Retrieves the view in the list corresponding to itemID */
public View getViewForID(long itemID) {
	int firstVisiblePosition = getFirstVisiblePosition();
	ListAdapter adapter = getAdapter();
	if (!adapter.hasStableIds()) {
		throw new IllegalStateException("Adapter doesn't have stable ids! Make sure your adapter has stable ids, and override hasStableIds() to return true.");
	}

	for (int i = 0; i < getChildCount(); i++) {
		View v = getChildAt(i);
		int position = firstVisiblePosition + i;
		long id = adapter.getItemId(position);
		if (id == itemID) {
			return v;
		}
	}
	return null;
}
 
Example 6
Source File: ExtendableListView.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
/**
 * Remember enough information to restore the screen state when the data has
 * changed.
 */
void rememberSyncState() {
    if (getChildCount() > 0) {
        mNeedSync = true;
        mSyncHeight = getHeight();
        // Sync the based on the offset of the first view
        View v = getChildAt(0);
        ListAdapter adapter = getAdapter();
        if (mFirstPosition >= 0 && mFirstPosition < adapter.getCount()) {
            mSyncRowId = adapter.getItemId(mFirstPosition);
        }
        else {
            mSyncRowId = NO_ID;
        }
        if (v != null) {
            mSpecificTop = v.getTop();
        }
        mSyncPosition = mFirstPosition;
    }
}
 
Example 7
Source File: ReorderedLayerView.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Stores a reference to the views above and below the item currently corresponding to the hover
 * cell. It is important to note that if this item is either at the top or bottom of the list,
 * mAboveItemId or mBelowItemId may be invalid.
 */
protected void updateNeighborViewsForID(long itemID)
{
    int position = getPositionForID(itemID);
    ListAdapter adapter = getAdapter();
    mAboveItemId = adapter.getItemId(position - 1);
    mBelowItemId = adapter.getItemId(position + 1);
}
 
Example 8
Source File: DynamicListView.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
/**
 * Stores a reference to the views above and below the item currently
 * corresponding to the hover cell. It is important to note that if this
 * item is either at the top or bottom of the list, mAboveItemId or mBelowItemId
 * may be invalid.
 */
private void updateNeighborViewsForId(long itemId) {
    int position = getPositionForId(itemId);
    ListAdapter adapter = getAdapter();
    if (!adapter.hasStableIds()) {
        throw new IllegalStateException("Adapter doesn't have stable ids! Make sure your adapter has stable ids, and override hasStableIds() to return true.");
    }

    mAboveItemId = position - 1 >= 0 ? adapter.getItemId(position - 1) : INVALID_ROW_ID;
    mBelowItemId = position + 1 < adapter.getCount() ? adapter.getItemId(position + 1) : INVALID_ROW_ID;
}
 
Example 9
Source File: MergeAdapter.java    From mimicry with Apache License 2.0 5 votes vote down vote up
/**
 * Get the row id associated with the specified position in the list.
 * @param position Position of the item whose data we want
 */
public long getItemId(int position) {
	for (ListAdapter piece : pieces) {
		int size = piece.getCount();

		if (position < size) {
			return (piece.getItemId(position));
		}

		position -= size;
	}

	return (-1);
}
 
Example 10
Source File: MergeAdapter.java    From SimpleExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the row id associated with the specified position in the list.
 *
 * @param position Position of the item whose data we want
 */
public long getItemId(int position) {
    for (ListAdapter piece : pieces) {
        int size = piece.getCount();

        if (position < size) {
            return piece.getItemId(position);
        }

        position -= size;
    }

    return -1;
}
 
Example 11
Source File: DynamicGridView.java    From DynamicGrid with Apache License 2.0 5 votes vote down vote up
public View getViewForId(long itemId) {
    int firstVisiblePosition = getFirstVisiblePosition();
    ListAdapter adapter = getAdapter();
    for (int i = 0; i < getChildCount(); i++) {
        View v = getChildAt(i);
        int position = firstVisiblePosition + i;
        long id = adapter.getItemId(position);
        if (id == itemId) {
            return v;
        }
    }
    return null;
}
 
Example 12
Source File: HListView.java    From Klyph with MIT License 5 votes vote down vote up
/**
 * Returns the set of checked items ids. The result is only valid if the choice mode has not been set to
 * {@link #CHOICE_MODE_NONE}.
 * 
 * @return A new array which contains the id of each checked item in the list.
 * 
 * @deprecated Use {@link #getCheckedItemIds()} instead.
 */
@Deprecated
public long[] getCheckItemIds() {
	// Use new behavior that correctly handles stable ID mapping.
	if ( mAdapter != null && mAdapter.hasStableIds() ) {
		return getCheckedItemIds();
	}

	// Old behavior was buggy, but would sort of work for adapters without stable IDs.
	// Fall back to it to support legacy apps.
	if ( mChoiceMode != ListView.CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null ) {
		final SparseBooleanArray states = mCheckStates;
		final int count = states.size();
		final long[] ids = new long[count];
		final ListAdapter adapter = mAdapter;

		int checkedCount = 0;
		for ( int i = 0; i < count; i++ ) {
			if ( states.valueAt( i ) ) {
				ids[checkedCount++] = adapter.getItemId( states.keyAt( i ) );
			}
		}

		// Trim array if needed. mCheckStates may contain false values
		// resulting in checkedCount being smaller than count.
		if ( checkedCount == count ) {
			return ids;
		} else {
			final long[] result = new long[checkedCount];
			System.arraycopy( ids, 0, result, 0, checkedCount );

			return result;
		}
	}
	return new long[0];
}
 
Example 13
Source File: ReorderedLayerView.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Retrieves the view in the list corresponding to itemID
 */
public View getViewForID(long itemID)
{
    int firstVisiblePosition = getFirstVisiblePosition();
    ListAdapter adapter = getAdapter();
    for (int i = 0; i < getChildCount(); i++) {
        View v = getChildAt(i);
        int position = firstVisiblePosition + i;
        long id = adapter.getItemId(position);
        if (id == itemID) {
            return v;
        }
    }
    return null;
}
 
Example 14
Source File: HListView.java    From letv with Apache License 2.0 5 votes vote down vote up
@Deprecated
public long[] getCheckItemIds() {
    if (this.mAdapter != null && this.mAdapter.hasStableIds()) {
        return getCheckedItemIds();
    }
    if (this.mChoiceMode == 0 || this.mCheckStates == null || this.mAdapter == null) {
        return new long[0];
    }
    SparseArrayCompat<Boolean> states = this.mCheckStates;
    int count = states.size();
    long[] ids = new long[count];
    ListAdapter adapter = this.mAdapter;
    int i = 0;
    int checkedCount = 0;
    while (i < count) {
        int checkedCount2;
        if (((Boolean) states.valueAt(i)).booleanValue()) {
            checkedCount2 = checkedCount + 1;
            ids[checkedCount] = adapter.getItemId(states.keyAt(i));
        } else {
            checkedCount2 = checkedCount;
        }
        i++;
        checkedCount = checkedCount2;
    }
    if (checkedCount == count) {
        return ids;
    }
    long[] result = new long[checkedCount];
    System.arraycopy(ids, 0, result, 0, checkedCount);
    return result;
}
 
Example 15
Source File: DynamicGridView.java    From Ninja with Apache License 2.0 5 votes vote down vote up
public View getViewForId(long itemId) {
    int firstVisiblePosition = getFirstVisiblePosition();
    ListAdapter adapter = getAdapter();
    for (int i = 0; i < getChildCount(); i++) {
        View v = getChildAt(i);
        int position = firstVisiblePosition + i;
        long id = adapter.getItemId(position);
        if (id == itemId) {
            return v;
        }
    }
    return null;
}
 
Example 16
Source File: DynamicListView.java    From Kore with Apache License 2.0 5 votes vote down vote up
/** Retrieves the view in the list corresponding to itemID */
public View getViewForID (long itemID) {
    int firstVisiblePosition = getFirstVisiblePosition();
    ListAdapter adapter = getAdapter();
    for(int i = 0; i < getChildCount(); i++) {
        View v = getChildAt(i);
        int position = firstVisiblePosition + i;
        long id = adapter.getItemId(position);
        if (id == itemID) {
            return v;
        }
    }
    return null;
}
 
Example 17
Source File: DynamicListView.java    From Kore with Apache License 2.0 5 votes vote down vote up
/**
 * Stores a reference to the views above and below the item currently
 * corresponding to the hover cell. It is important to note that if this
 * item is either at the top or bottom of the list, mAboveItemId or mBelowItemId
 * may be invalid.
 */
private void updateNeighborViewsForID(long itemID) {
    int position = getPositionForID(itemID);
    ListAdapter adapter = getAdapter();
    mAboveItemId = adapter.getItemId(position - 1);
    mBelowItemId = adapter.getItemId(position + 1);
}
 
Example 18
Source File: MergeAdapter.java    From AndroidBleManager with Apache License 2.0 5 votes vote down vote up
/**
 * Get the row id associated with the specified position
 * in the list.
 *
 * @param position
 *          Position of the item whose data we want
 */
@Override
public long getItemId(int position) {
    for (ListAdapter piece : getPieces()) {
        int size=piece.getCount();

        if (position < size) {
            return(piece.getItemId(position));
        }

        position-=size;
    }

    return(-1);
}
 
Example 19
Source File: MergeAdapter.java    From BLE with Apache License 2.0 5 votes vote down vote up
public long getItemId(int position) {
    int size;
    for (Iterator i$ = this.getPieces().iterator(); i$.hasNext(); position -= size) {
        ListAdapter piece = (ListAdapter) i$.next();
        size = piece.getCount();
        if (position < size) {
            return piece.getItemId(position);
        }
    }

    return -1L;
}
 
Example 20
Source File: ScrollState.java    From Onosendai with Apache License 2.0 5 votes vote down vote up
private void applyToListView (final ListView lv) {
	// NOTE if this seems unreliable try wrapping setSelection*() calls in lv.post(...).
	final ListAdapter adapter = lv.getAdapter();

	if (this.itemId >= 0L) {
		for (int i = 0; i < adapter.getCount(); i++) {
			if (adapter.getItemId(i) == this.itemId) {
				lv.setSelectionFromTop(i, this.top);
				return;
			}
		}
	}

	// Also search by time before giving up.
	if (this.itemTime > 0L && adapter instanceof TweetListCursorAdapter) {
		final TweetListCursorAdapter tlca = (TweetListCursorAdapter) adapter;
		for (int i = 0; i < tlca.getCount(); i++) {
			final long itime = tlca.getItemTime(i);
			if (itime > 0L && itime <= this.itemTime) {
				lv.setSelectionFromTop(i, 0);
				return;
			}
		}
		LOG.w("Failed to restore scroll state %s to list of %s items.", this, tlca.getCount());
	}
	else {
		LOG.w("Failed to restore scroll state %s.", this);
	}

	lv.setSelection(lv.getCount() - 1);
}