java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1 in recyclerview adapter

烂漫一生 提交于 2020-01-16 15:18:19

问题


I am using a recycler view. Here is the 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();
}}

I am getting IndexOutofBoundException although I have not inserted any item in the list.

Here is the error:

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

It was working fine when the dialog box to delete item was in MainActivity, but I had to reload the activity to show that an item had been deleted.Now this error appears whenever the MainActivity starts. Can anyone tell me if there is any other way in which I can set a longOnClickListener on recycler view item which displays a dialog box,which deletes the item from the list on clicking OK and also reflects that the item has been deleted in the MainActivity?


回答1:


Problem in this line .

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

You are using getAdapterPosition() in constructor thats why its throwing -1. Instead of it you can implements onLongClickListener in your Holder class .

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


来源:https://stackoverflow.com/questions/47904377/java-lang-arrayindexoutofboundsexception-length-12-index-1-in-recyclerview-ad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!