gpt4 book ai didi

java.lang.IndexOutOfBoundsException : Invalid index 20, 大小为 20

转载 作者:行者123 更新时间:2023-12-01 13:54:39 25 4
gpt4 key购买 nike

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

}

这是设置 ListView 的自定义适配器:

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;
}
}

最佳答案

@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

关于java.lang.IndexOutOfBoundsException : Invalid index 20, 大小为 20,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19688082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com