What's the purpose of item-id's in Android ListView Adapter?

前端 未结 5 1701
[愿得一人]
[愿得一人] 2020-11-29 08:37

(Not specific to ListView, but to Adapter).

I keep implementing this when I subclass BaseAdapter:

    @Override
    public long getItemId(int positio         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 09:11

    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 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.

    Few reasons I have come across:

    • AdapterView.OnItemClickListener's method onItemClick(AdapterView parent, View view, int position, long id) also sends long id

    • the getCheckedItemIds method The result is only valid if the choice mode has not been set to CHOICE_MODE_NONE and the adapter has stable IDs.

    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.

提交回复
热议问题