0

我正在使用自定义适配器从 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;
    }
}
4

1 回答 1

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
于 2013-10-30T16:11:58.867 回答