java.lang.IndexOutOfBoundsException:索引20无效,大小为20

时间:2013-10-30 16:09:42

标签: java android android-adapter

我正在使用自定义适配器从JSON字符串加载20个项目,并在第4和第14位置实施广告。我遇到的是当我滚动查看项目#19时,它会因标题中的错误而崩溃。

我知道错误是说它正在尝试访问不在数组中的索引,但我认为这是因为它包含了索引中的adViews(它不应该)。我觉得这很简单,我想念但请帮助。这是我的2个适配器:

public class ListViewAdapter extends BaseAdapter implements AdListener {

private final Activity activity;
private final BaseAdapter delegate;

public ListViewAdapter(Activity activity, BaseAdapter delegate) {
    this.activity = activity;
    this.delegate = delegate;
}

@Override
public int getCount() {
    // Total count includes list items and ads.
    return delegate.getCount() + 2;
}

@Override
public Object getItem(int position) {
    // Return null if an item is an ad.  Otherwise return the delegate item.
    if (isItemAnAd(position)) {
        return null;
    }
    return delegate.getItem(position - 1);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (isItemAnAd(position)) {
        if (convertView instanceof AdView) {
            return convertView;
        } else {
            AdView adView = new AdView(activity, AdSize.SMART_BANNER,"AD ID (removed for post)" );
            AdRequest adRequest = new AdRequest();
            adView.loadAd(adRequest);
            return adView;
        }
    } else {
        return delegate.getView(position-1, convertView, parent);
    }
}

@Override
public int getViewTypeCount() {
    return delegate.getViewTypeCount() + 1;
}

@Override
public int getItemViewType(int position) {
    if (isItemAnAd(position)) {
        return delegate.getViewTypeCount();
    } else {
        return delegate.getItemViewType(getOffsetPosition(position));
    }
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return (!isItemAnAd(position)) && delegate.isEnabled(getOffsetPosition(position));
}

private boolean isItemAnAd(int position) {
    // Place an ad at the first and last list view positions.
    return (position == 4 || position == 14);
}

@Override
public void onDismissScreen(Ad arg0) {
}

@Override
public void onFailedToReceiveAd(Ad arg0, AdRequest.ErrorCode arg1) {
}

@Override
public void onLeaveApplication(Ad arg0) {
}

@Override
public void onPresentScreen(Ad arg0) {
}

@Override
public void onReceiveAd(Ad arg0) {

}

private int getOffsetPosition(int position) {
    return position - 1;
}

}

这是设置列表视图的自定义适配器:

class CustomMovieAdapter extends BaseAdapter {

    private ArrayList<SearchResults> searchArrayList;

    private LayoutInflater mInflater;

    public CustomMovieAdapter(Context context, ArrayList<SearchResults> results){

        searchArrayList = results;
        mInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return searchArrayList.size()+2;
    }

    @Override
    public Object getItem(int position) {
        return searchArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();

            if(convertView == null){
                convertView = mInflater.inflate(R.layout.list_item, null);
                holder = new ViewHolder();

                assert convertView != null;
                holder.txtRating = (TextView) convertView.findViewById(R.id.movieRating);
                holder.txtMovieName = (TextView) convertView.findViewById(R.id.movieName);
                holder.txtMovieSize = (TextView) convertView.findViewById(R.id.movieSize);
                holder.txtImdbRating = (TextView) convertView.findViewById(R.id.imdbScore);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtRating.setText(searchArrayList.get(position+1).getRating());
            holder.txtMovieName.setText(searchArrayList.get(position+1).getName());
            holder.txtMovieSize.setText(searchArrayList.get(position+1).getSize());
            holder.txtImdbRating.setText(searchArrayList.get(position+1).getImdbRating());

        return convertView;

    }
    class ViewHolder {

        TextView txtRating;
        TextView txtMovieName;
        TextView txtMovieSize;
        TextView txtImdbRating;
    }
}

1 个答案:

答案 0 :(得分:1)

@Override
public int getCount() {
    // Total count includes list items and ads.
    return delegate.getCount() + 2;
}

您应该返回数据集的大小而不更改它。如果需要显示更多项目,请将其添加到数据集中。用

更改它
@Override
public int getCount() {
    // Total count includes list items and ads.
    return delegate.getCount() ;
}

应该修复你的例外。

编辑,在getView中你应该避免篡改android为你提供的位置:

position+1