Reputation:
I have a custom base adapter to work with my ArrayList of customObjects. There's a small chance that my data set is changing in the background when the user enters my listView and it seems to be causing my IndexOutOfBoundsException. I'm currently doing:
@Override
public long getItemId(int position) {
return custom.get(position).getID();
}
Should I just stick to returning the position from like ArrayAdapter?
/**
* {@inheritDoc}
*/
public long getItemId(int position) {
return position;
}
I could just catch this error, but I'm not sure what to do in the catch. I need to return something, but I'm afraid to return something like -1 or 0.
In this question: What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?
People say that ArrayAdapter returns -1, but that's not the case (as of March 2014 at least).
Upvotes: 0
Views: 1159
Reputation: 1015
Just check that your position is less than the size of the array, and return -1 if so.
@Override
public long getItemId(int position) {
if(position < custom.size()){
return custom.get(position).getID();
}
return -1;
}
Upvotes: 1