Indexoutofboundsexception with listview

后端 未结 5 350
抹茶落季
抹茶落季 2020-12-21 06:28

I just got a strange indexoutofboundsexception that I can\'t seem to reproduce. It seems to be related to my listactivity:

java.lang.IndexOutOfBoundsExceptio         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-21 07:09

    I solve this problem by make sure notify adapter when data set is changed. In my case,data was changed by another thread,after listview check if data was right. After look into listview source. I fount the exception throw out here:

        public View More ...getView(int position, View convertView, ViewGroup parent) {
        // Header (negative positions will throw an ArrayIndexOutOfBoundsException)
        int numHeaders = getHeadersCount();
        if (position < numHeaders) {
            return mHeaderViewInfos.get(position).view;
        }
    
        // Adapter
        final int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (mAdapter != null) {
            adapterCount = mAdapter.getCount();
            if (adjPosition < adapterCount) {
                return mAdapter.getView(adjPosition, convertView, parent);
            }
        }
    
        // Footer (off-limits positions will throw an ArrayIndexOutOfBoundsException)
        return mFooterViewInfos.get(adjPosition - adapterCount).view;
    }
    

    So you can see that mFooterViewInfos.get(adjPosition - adapterCount).view throw a Indexoutofboundsexception. Because position is larger than listview thought it will be.

    What you have to do is find out whitch view cause this exception. And make sure once the data changed notify the adapter immediately!

提交回复
热议问题