Ran*_*ku' 12 android listview position adapter
(不是特定于ListView,而是适配器).
当我继承BaseAdapter时,我一直在实现这个:
@Override
public long getItemId(int position) {
return position;
}
Run Code Online (Sandbox Code Playgroud)
因为必须实现这一点.我没有看到任何使用它,我只需要getItem(位置),而不是getItemId(位置).
我想知道它是否有任何意义(Android SDK或其他东西)?
Gra*_*tei 31
你有db表注释这样的3条记录:
+----+--------------------------+
| ID | Note Text |
+----+--------------------------+
| 43 | Note text blah blah |
| 67 | Note text blah blah blah |
| 85 | Last note |
+----+--------------------------+
Run Code Online (Sandbox Code Playgroud)
并实现适配器来提供此数据.
position - 是已加载数据集中记录位置的序数.例如,如果您使用ORDER BY ID ASC,那么加载该表
itemId - 是记录的"主键",您的实现可以返回这些值
in ArrayAdapter和SimpleAdapterposition和itemId是一回事:
public long getItemId(int position) {
return position;
}
Run Code Online (Sandbox Code Playgroud)
在SimpleCursorAdapter和CursorAdapter的所有后代中,itemId是来自_id列的值:
public long getItemId(int position) {
if (mDataValid && mCursor != null) {
if (mCursor.moveToPosition(position)) {
return mCursor.getLong(mRowIDColumn);
} else {
return 0;
}
} else {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
拥有稳定的物品 ID 的原因可能有很多。无法给出默认实现,因为它取决于存储在适配器中的对象类型。
Android 有一项检查,以确保项目 ID 仅在稳定时才使用,即子类已正确覆盖getItemId;必须重写BaseAdapter.hasStableIds才能返回 true。
我遇到的几个原因:
AdapterView.OnItemClickListener的方法onItemClick(AdapterView<?> parent, View view, int position, long id)也发送long id
getCheckedItemIds方法The result is only valid if the choice mode has not been set to CHOICE_MODE_NONE and the adapter has stable IDs.
回复“因为必须实现这个”:你不必如果你不使用这些功能,那就没有必要。boolean hasStableIds()但如果你这样做了,也不要忘记覆盖。
| 归档时间: |
|
| 查看次数: |
14775 次 |
| 最近记录: |