java.lang.ArrayIndexOutOfBoundsException:length = 12; recyclerview adapter

时间:2017-12-20 10:53:05

标签: android android-recyclerview indexoutofboundsexception recycler-adapter

我正在使用回收者视图。 这是适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Contact> myContacts;
private Activity activity;

public MyAdapter(List<Contact> contacts,Activity activity)
{
    this.myContacts = contacts;
    this.activity = activity;
}


public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView mTextView;
    public ImageView mColorView;
    public LinearLayout linearLayout;
    public ViewHolder(View v,MyAdapter adapter) {
        super(v);
        mTextView =  v.findViewById(R.id.name);
        mColorView = v.findViewById(R.id.image);
        linearLayout = v.findViewById(R.id.listItemLinearLayout);
        v.setOnLongClickListener(onLongClickListener(myContacts.get(getAdapterPosition()).getID(),getAdapterPosition(),adapter));
    }
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
    // set the view's size, margins, paddings and layout parameters
    ViewHolder vh = new ViewHolder(v,this);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    String name = myContacts.get(position).getName();
    TextDrawable myDrawable = TextDrawable.builder().beginConfig()
            .textColor(Color.WHITE)
            .useFont(Typeface.DEFAULT)
            .toUpperCase()
            .endConfig()
            .buildRound(name.substring(0,1),myContacts.get(position).getColor());

    holder.mColorView.setImageDrawable(myDrawable);
    holder.mTextView.setText(myContacts.get(position).getName());
    holder.linearLayout.setBackgroundColor(Color.DKGRAY);

}

private View.OnLongClickListener onLongClickListener(final long position, final int pos,final MyAdapter adapter){
    return new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity, R.style.MyDialogTheme);
            alertDialog.setCancelable(false);
            alertDialog.setMessage("Delete item?");
            alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    LockedContacts.deleteTitle(position,activity);
                    myContacts.remove(pos);
                    notifyItemRemoved(pos);
                    notifyItemRangeChanged(pos, myContacts.size());
                }
            });
            alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();

                }
            });
            AlertDialog alert11 = alertDialog.create();
            alert11.show();

            return true;
        }
    };
}


@Override
public int getItemCount() {
    Log.d("SIZE",String.valueOf(myContacts.size()));
    return myContacts.size();
}}

虽然我没有在列表中插入任何项目,但我收到了IndexOutofBoundException。

这是错误:

Process: com.example.ark.AccessibilityService, PID: 17104                                                                                  
java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
at java.util.ArrayList.get(ArrayList.java:310)
at com.example.ark.access.MyAdapter$ViewHolder.<init>(MyAdapter.java:46)
at com.example.ark.access.MyAdapter.onCreateViewHolder(MyAdapter.java:57)
at com.example.ark.access.MyAdapter.onCreateViewHolder(MyAdapter.java:27)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5443)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:869)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5443)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5443)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5443)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5443)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5443)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5443)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2733)
at android.view.View.layout(View.java:16668)
at android.view.ViewGroup.layout(ViewGroup.java:5443)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6042)
android.view.Choreographer$CallbackReco

当删除项目的对话框在MainActivity中时,它工作正常,但我不得不重新加载活动以显示项目已被删除。现在每当MainActivity启动时都会出现此错误。任何人都可以告诉我是否有其他方法可以在Recycler视图项目上设置longOnClickListener,该项目显示一个对话框,单击确定后从列表中删除该项目,并且还反映该项目已在MainActivity中删除?

1 个答案:

答案 0 :(得分:2)

这一行有问题。

v.setOnLongClickListener(onLongClickListener(myContacts.get(getAdapterPosition()).getID(),getAdapterPosition(),adapter));

您在构造函数中使用getAdapterPosition(),这就是为什么抛出-1。 您可以在Holder类中实现onLongClickListener,而不是它。

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
    public TextView mTextView;
    public ImageView mColorView;
    public LinearLayout linearLayout;
    public ViewHolder(View v,MyAdapter adapter) {
        super(v);
        mTextView =  v.findViewById(R.id.name);
        mColorView = v.findViewById(R.id.image);
        linearLayout = v.findViewById(R.id.listItemLinearLayout);
        v.setOnLongClickListener(this);
    }
    @Override
    public boolean onLongClick(View v) {
        if(getAdapterPosition()!=-1) {
            // Do your stuff here
            onLongClickListener(myContacts.get(getAdapterPosition()).getID(), getAdapterPosition(), adapter)
        }
        return false;
    }
}