Android ListView Adapter 中 item-id 的目的是什么? [英] What's the purpose of item-id's in Android ListView Adapter?
问题描述
(不是特定于 ListView,而是特定于 Adapter).
(Not specific to ListView, but to Adapter).
当我继承 BaseAdapter 时,我一直在实现它:
I keep implementing this when I subclass BaseAdapter:
@Override
public long getItemId(int position) {
return position;
}
因为必须实现它.我没有看到它的任何用途,我只需要 getItem(position),而不是 getItemId(position).
Because have to implement that. I don't see any use of it, I need getItem(position) only, not getItemId(position).
我想知道它是否有任何意义(对 Android SDK 或其他东西)?
I wonder if it has any significance (to Android SDK or something else)?
推荐答案
拥有 稳定 项目 ID 的原因可能有很多.无法给出默认实现,因为它取决于存储在适配器中的对象类型.
There are probably many reasons to have stable item IDs. A default implementation cannot be given since it depends on the type of Object being stored in the Adapter.
Android 有一项检查,以确保仅在稳定后才使用项目 ID,即子类已正确覆盖 getItemId
;BaseAdapter.hasStableIds 必须被覆盖才能返回 true.
Android has a check to make sure that item IDs are used only when they are stabled, i.e. the subclass has properly overridden getItemId
; BaseAdapter.hasStableIds must be overriden to return true.
我遇到的几个原因:
AdapterView.OnItemClickListener 的方法
onItemClick(AdapterView> parent, View view, int position, long id)
也发送long id
getCheckedItemIds 方法 结果只有有效,如果选择模式尚未设置为Choice_Mode_None,并且适配器具有稳定的ID.
回复因为必须实现那个":您不必必须如果您不使用这些功能,则没有必要.但是如果你这样做了,不要忘记覆盖 boolean hasStableIds()
.
Reply to "Because have to implement that": you don't have to If you don't use the features, there's no need. But if you do, don't forget to override boolean hasStableIds()
as well.
这篇关于Android ListView Adapter 中 item-id 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!