Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to make an adapter with stable ids?

I've made my own custom adapter extended from BaseAdapter to show a listview and so on...

I want it to support single and multi selection, so it must have stable ids. I've checked with the ADAPTER.hasStableIds() and the result is false.

I've overrided this method to do try to get stables ids with no luck.

public long getItemId(int position) {
   return (long) getItem(position).hashCode();
}

Any idea how to make it? thanks!

like image 215
giorgiline Avatar asked Apr 22 '12 11:04

giorgiline


2 Answers

Override hasStableIds to return true.

Also the data on your adapter must either override hashCode() or has some kind of id field to be returned on getItemId.

like image 187
rui.araujo Avatar answered Oct 29 '22 14:10

rui.araujo


Add this in your adapter:

init {    
    setHasStableIds(true)
}

override fun getItemId(position: Int): Long = position.toLong()
like image 3
Muhammed naseef Koppilakkal Avatar answered Oct 29 '22 15:10

Muhammed naseef Koppilakkal