getAdapterPosition在ViewHolder类中返回-1 [英] getAdapterPosition returns -1 in ViewHolder class

查看:287
本文介绍了getAdapterPosition在ViewHolder类中返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的ViewHolder类中显示一个AlertDialog,然后单击接受按钮后,我从项目列表中获得了带有getAdapterPosition的模型项目,但是在Fabric Crashlytics中,我发生了13次崩溃,因为ArrayIndexOutOfBoundsException的长度表示长度为12,但请求的索引为-1,而崩溃是针对此部分代码中的getPaymentMode

I'm trying to show an AlertDialog in my ViewHolder class and after clicking on accept button I'm getting the Model item with getAdapterPosition from a list of items but in Fabric Crashlytics I have 13 crashes because of ArrayIndexOutOfBoundsException which says length is 12 but the index requested is -1 and the crash is for getPaymentMode in this part of code

class ViewHolder extends RecyclerView.ViewHolder {
    TextView time, capacity, description;
    View button;
    ImageView avatar;

    ViewHolder(View v) {
        super(v);
        time = v.findViewById(R.id.reserve_times_time);
        capacity = v.findViewById(R.id.reserve_times_capacity);
        button = v.findViewById(R.id.button);
        description = v.findViewById(R.id.reserve_times_description);
        avatar = v.findViewById(R.id.avatar);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
                alertDialogBuilder.setView(R.layout.layout_dialog);
                alertDialogBuilder.setPositiveButton("accept", null);
                alertDialogBuilder.setNegativeButton("cancel", null);
                final AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
                alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.dialog_button_text_size));
                alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.dialog_button_text_size));
                alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                        getPaymentMode(arrayList.get(getAdapterPosition()), button);
                    }
                });
            }
        });
    }

当所有者RecyclerView为空时,RecyclerView源代码getAdapterPosition中的

返回-1,如果活动被关闭,会发生这种情况,但是怎么发生呢?当AlertDialog显示用户时,无法关闭活动!

in RecyclerView source code getAdapterPosition returns -1 when owner RecyclerView is null and that will happen if activity is closed but how this can be happened? when AlertDialog is displaying user can't close activity!

推荐答案

根据

According to the docs, getAdapterPosition() will return NO_POSITION (aka -1) if your view holder has already been recycled.

项目的适配器位置(如果适配器中仍然存在). NO_POSITION:如果已从适配器中删除项目,则在最后一次布局传递或ViewHolder已回收之后,将调用notifyDataSetChanged().

The adapter position of the item if it still exists in the adapter. NO_POSITION if item has been removed from the adapter, notifyDataSetChanged() has been called after the last layout pass or the ViewHolder has already been recycled.

我的猜测是,当您单击对话框按钮时,视图持有者已经被回收.尝试在onClick()方法开始时正确存储位置,然后在需要时使用它,例如:

My guess is that by the time you're clicking on your dialog button, your view holder has already been recycled. Try to store the position right when your onClick() method begins and then use it when you need it, something like:

button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    final int position = getAdapterPosition()
    //Your code here             
    alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        alertDialog.dismiss();
        getPaymentMode(arrayList.get(position), button);
      }
    });
  }
});

这篇关于getAdapterPosition在ViewHolder类中返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆