下面列出了android.widget.ListAdapter#getItemId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
}
/**
* 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;
}
/** 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;
}
/**
* 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;
}
}
/**
* 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;
}
/**
* 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;
}
@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;
}
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);
}
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;
}
/**
* 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);
}
/**
* 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);
}
/** 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;
}
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;
}
/**
* 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);
}
/**
* 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;
}
/**
* 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];
}
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;
}
/**
* 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;
}
/**
* 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);
}
/**
* 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;
}