0

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

Community
  • 1
  • 1

1 Answers1

1

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;
}
pat
  • 1,005
  • 4
  • 12
  • 29
  • Don't believe you can return null when it is expecting a primitive. –  Mar 12 '14 at 15:53
  • Interesting. Do you know why this method even exists? Doesn't seem to be used for anything. –  Mar 13 '14 at 01:53