[英]IndexOutOfBoundsException in getItemId in Android baseAdapter
I have a custom base adapter to work with my ArrayList of customObjects. 我有一个自定义基本适配器,可与我的customObjects ArrayList一起使用。 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.
当用户进入我的listView时,我的数据集在后台发生变化的可能性很小,这似乎导致了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? 我应该坚持像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.
我需要返回一些东西,但是我害怕返回-1或0之类的东西。
In this question: What is the intent of the methods getItem and getItemId in the Android class BaseAdapter? 在这个问题中: Android类BaseAdapter中的getItem和getItemId方法的目的是什么?
People say that ArrayAdapter returns -1, but that's not the case (as of March 2014 at least). 人们说ArrayAdapter返回-1,但事实并非如此(至少从2014年3月开始)。
Just check that your position is less than the size of the array, and return -1 if so. 只需检查您的位置是否小于数组的大小,然后返回-1即可。
@Override
public long getItemId(int position) {
if(position < custom.size()){
return custom.get(position).getID();
}
return -1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.