问题描述
我试图在我的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!
推荐答案
根据文档,如果视图持有者已经被回收,则getAdapterPosition()
将返回NO_POSITION
(又名-1).
According to the docs, getAdapterPosition()
will return NO_POSITION
(aka -1) if your view holder 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!