RecyclerView的holder.getAdapterPosition()返回-1。导致ArrayIndexOutOfBoundsException

时间:2019-04-03 05:28:33

标签: android android-recyclerview adapter

我正在尝试在OnClickListner recyclerView内的项目上添加adapter,其余功能可以与holder.getAdapterPosition()配合使用,但是在尝试实现{{ 1}}上显示onClickListner错误。 我正在使用ArrayIndexOutOfBoundsException

My_Adapter_Class

FlexboxLayoutManager

Crash_Log

public class SuggestedSkillAdapter extends RecyclerView.Adapter<SuggestedSkillAdapter.ViewHolder> {
public List<String> suggestedSkills;
public List<String> filterList;
Context context;
private int itemlayout;
FragOne fragOne;

public SuggestedSkillAdapter(int skill_badge_layout, List<String> suggestedSkills, Context context, FragOne fragOne) {
    this.itemlayout = skill_badge_layout;
    this.context = context;
    this.suggestedSkills = suggestedSkills;
    this.filterList = suggestedSkills;
    this.fragOne = fragOne;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(itemlayout, parent, false);
    ViewHolder myViewHolder = new ViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.skillName.setText(suggestedSkills.get(holder.getAdapterPosition()));
    holder.cardView.setOnClickListener(view -> {
        fragOne.addItemToSkillsToBeReviewed(suggestedSkills.get(holder.getAdapterPosition()));
        suggestedSkills.remove(holder.getAdapterPosition());
        notifyItemRemoved(holder.getAdapterPosition());
        Toast.makeText(context, "" + holder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
    });
}

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

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

public void filterList(ArrayList<String> filterdNames) {
    this.suggestedSkills = filterdNames;
    notifyDataSetChanged();
    notifyItemRangeChanged(0, suggestedSkills.size());
}

public class ViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.skillName)
    TextView skillName;
    @BindView(R.id.cardMain)
    CardView cardView;

    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

}
}

一旦开始出现错误,我将检查Toast消息上的位置。但是从相同的方法调用list的值,并且值按预期方式显示。无法解决为何将-1显示为位置的原因。

我希望`2019-04-03 11:04:10.774 18117-18117/com.example.addreview D/AndroidRuntime: Shutting down VM 2019-04-03 11:04:10.780 18117-18117/com.example.addreview E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.addreview, PID: 18117 java.lang.ArrayIndexOutOfBoundsException: length=196; index=-1 at java.util.ArrayList.get(ArrayList.java:439) at com.example.addreview.Adapters.SuggestedSkillAdapter.lambda$onBindViewHolder$0(SuggestedSkillAdapter.java:55) at com.example.addreview.Adapters.-$$Lambda$SuggestedSkillAdapter$ib8AJ50Na4r0wQZw7b9CrGCGhkY.onClick(Unknown Source:4) at android.view.View.performClick(View.java:6600) at android.view.View.performClickInternal(View.java:6577) at android.view.View.access$3100(View.java:781) at android.view.View$PerformClick.run(View.java:25912) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6912) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)` 的确切位置

3 个答案:

答案 0 :(得分:2)

在适配器内部创建一个自定义侦听器,然后将事件向上触发到父级中定义的接口实现。您的点击逻辑应在包含的“活动”或“片段”中进行处理。

@Override
 public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
 // handle event inside onBindViewHolder  
 holder.someButtonToRemoveItem.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       // some listener interface method
         onRemoveClickListener.onClick(view, position);
    }
});
}

Attaching click handlers using listeners within the RecyclerView

答案 1 :(得分:1)

您不应该在onBindViewHolder()上执行dataSet操作。 它返回-1,因为recyclerview正在计算布局测量值。

即使ClickListeners应该在ViewHolder类中定义,因为onBindViewHolder()总是被调用,因此,当我们谈论性能方面时,视图操作将很昂贵。

建议: 将整个onClickListener代码段移至ViewHolder类,看看会发生什么。使用getAdapterPosition()的{​​{1}}方法获取View的当前位置。

答案 2 :(得分:0)

使用从onBindViewHolder接收的“位置”值代替getAdapterPosition()。