如何修复IndexOutOfBoundsException:索引:1,大小:1在android中

时间:2017-09-05 09:01:05

标签: android list arraylist android-adapter

在我的应用程序中,我想从服务器获取一些列表数据,并将其设置为显示项目的适配器。 我写下面的代码,但运行时,应用程序会显示强制关闭错误,并在Logcat中显示此错误。

错误消息:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:437)
at app.test.Adapters.CelebrityDetailAdapter.AwardGoldenAdapter.onBindViewHolder(AwardGoldenAdapter.java:53)
at app.test.Adapters.CelebrityDetailAdapter.AwardGoldenAdapter.onBindViewHolder(AwardGoldenAdapter.java:23)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
at android.view.View.layout(View.java:19393)
at android.view.ViewGroup.layout(ViewGroup.java:6022)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
at android.view.View.layout(View.java:19393)
at android.view.ViewGroup.layout(ViewGroup.java:6022)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)

适配器代码:

public class AwardGoldenAdapter extends    RecyclerView.Adapter<AwardGoldenAdapter.ViewHolder> {

private Context context;
private List<Award> model;
private int modelImage;
private int won, nominated, total;

public AwardGoldenAdapter(Context context, List<Award> model, int won, int nominated) {
    this.context = context;
    this.model = model;
    this.won = won;
    this.nominated = nominated;

    if (won > 0) {
        total = won;
    } else {
        total = nominated;
    }
}

@Override
public AwardGoldenAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_award, parent, false);

    return new AwardGoldenAdapter.ViewHolder(view);
}

@Override
public void onBindViewHolder(final AwardGoldenAdapter.ViewHolder holder, final int position) {

    if (model.get(position).getWon()) {
        modelImage = R.drawable.golden_globe_gold;
    } else {
        modelImage = R.drawable.golden_globe_silver;
    }
    Glide.with(context)
            .load(modelImage)
            .placeholder(R.drawable.default_image)
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(holder.row_awardImg);
}

@Override
public int getItemCount() {
    return total;
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private ImageView row_awardImg;

    public ViewHolder(View itemView) {
        super(itemView);

        row_awardImg = (ImageView) itemView.findViewById(R.id.row_awardImg);

    }
}
}

从服务器获取数据的活动中的代码(对于我使用Retrofit的服务器连接):

    private void getGoldenGlobeData() {
    celebrityAwardSendData = new CelebrityAwardSendData();
    celebrityAwardSendData.setCelebrityID(celebrityID);

    InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
    Call<CelebrityAwardResponse> call = api.getCelebrityAward(celebrityAwardSendData);

    call.enqueue(new Callback<CelebrityAwardResponse>() {
        @Override
        public void onResponse(Call<CelebrityAwardResponse> call, Response<CelebrityAwardResponse> response) {
            if (response.body().getData() != null) {
                awardModel.clear();
                List<Award> won = new ArrayList<>();
                List<Award> lost = new ArrayList<>();
                for (Award award : response.body().getData().get(1).getAwards()) {
                    if (!award.getWon()) {
                        lost.add(award);
                    } else {
                        won.add(award);
                    }
                }
                if (won.size() > 0) {
                    awardModel.addAll(won);
                } else {
                    awardModel.addAll(lost);
                }

                awardGoldenAdapter = new AwardGoldenAdapter(context, awardModel, won.size(), lost.size());
                infoMovieFrag_AwardGoldenRecyclerView.setLayoutManager(new LinearLayoutManager
                        (context, LinearLayoutManager.HORIZONTAL, false));
                infoMovieFrag_AwardGoldenRecyclerView.setHasFixedSize(true);
                infoMovieFrag_AwardGoldenRecyclerView.setNestedScrollingEnabled(false);
                infoMovieFrag_AwardGoldenRecyclerView.addOnItemTouchListener(disableRecyclerViewScroll);
                infoMovieFrag_AwardGoldenRecyclerView.setAdapter(awardGoldenAdapter);
            }
        }

        @Override
        public void onFailure(Call<CelebrityAwardResponse> call, Throwable t) {
        }
    });
}

在适配器中显示此行的错误:

if (model.get(position).getWon()) {

如何修复此错误?

4 个答案:

答案 0 :(得分:3)

在您的活动中添加此代码

 if (won.size() > 0) {
       awardModel.addAll(won);
       awardGoldenAdapter = new AwardGoldenAdapter(context, awardModel);
    } else {
       awardModel.addAll(lost);
       awardGoldenAdapter = new AwardGoldenAdapter(context, awardModel);
    }

并像这样更改你的构造函数

public AwardGoldenAdapter(Context context, List<Award> model) {
    this.context = context;
    this.model = model;
}

更改getItemCount()中的AwardGoldenAdapter方法,如下面的代码

@Override
public int getItemCount() {
    return model.size();
}

答案 1 :(得分:2)

在活动中尝试此操作:

if (won.size() > 0) {
   awardModel.addAll(won);
   awardGoldenAdapter = new AwardGoldenAdapter(context, awardModel);
} else {
   awardModel.addAll(lost);
   awardGoldenAdapter = new AwardGoldenAdapter(context, awardModel);
}

并且还按以下方式更改适配器:

public AwardGoldenAdapter(Context context, List<Award> model) {
   this.context = context;
   this.model = model;
}

@Override
public int getItemCount() {
    return this.model.size();
}

答案 2 :(得分:1)

要删除您的错误,只需更改getItemCount() .内的一条语句 return model.size()

答案 3 :(得分:0)

只需添加notifyDataSetChanged();删除或添加后添加到您的代码中