我尽力了解这个问题的问题。我正在创建一个涉及编辑recyclerview
的内容(项目)的函数。在编辑一行时它可以正常工作,但在编辑ArrayIndexOutOfBounds
中的另一行时会抛出recyclerview
异常。
此代码是错误的来源。
((ClassListOnSetup) context).editClassnameAt(classes.get(getAdapterPosition()).getId(), getAdapterPosition(), classesBean);
我认为getAdapterPosition()
方法可能会抛出此错误:
以下是editClassnameAt()
方法以供参考:
public void editClassnameAt(long id,int position, String classname ){
demeaSQL.updateAclass(id,classname);
classListOnSetupRecyclerAdapter.notifyItemChanged(position);
classListOnSetupRecyclerAdapter.notifyDataSetChanged();
displayClassList();
}
LogCat
的错误消息java.lang.ArrayIndexOutOfBoundsException:length = 10;指数= -1
在java.util.ArrayList.get(ArrayList.java:413)at com.example.demeainc.demea.ClassListOnSetupRecyclerAdapter $ ClassListOnSetupViewHolder $ 2 $ 2.onClick(ClassListOnSetupRecyclerAdapter.java:87) 在 android.support.v7.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:162)
我错了什么。有什么帮助吗?
对于那些想要查看完整代码的人,
这是我的Adapter类完整代码:
public class ClassListOnSetupRecyclerAdapter extends RecyclerView.Adapter<ClassListOnSetupRecyclerAdapter.ClassListOnSetupViewHolder {
//simple list of classes(mDataSet for Adapter)
private List<ClassesBean> listClasses;
private Context mContext;
public class ClassListOnSetupViewHolder extends RecyclerView.ViewHolder {
public TextView textView_classname;
public Button delete_button_classsetup;
public ClassListOnSetupViewHolder(final Context context, View itemView, final List<ClassesBean> classes) {
super(itemView);
textView_classname = itemView.findViewById(R.id.textView_classname);
delete_button_classsetup = itemView.findViewById(R.id.delete_button_classsetup);
delete_button_classsetup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// if(view.equals(delete_button_classsetup)) {
Log.i("Deletes", "Button pressed");
long id = classes.get(getAdapterPosition()).getId();
((ClassListOnSetup) context).removeAt(getAdapterPosition(), classes.get(getAdapterPosition()).getId());
Toast.makeText(context, "You Clicked ID = " + id, Toast.LENGTH_SHORT).show();
}
});
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View promptView = inflater.inflate(R.layout.edit_class, null);
AlertDialog.Builder alertDialogueBuilder = new AlertDialog.Builder(mContext);
alertDialogueBuilder.setView(promptView);
final EditText editText = promptView.findViewById(R.id.edittable_className);
editText.setText((listClasses.get(getAdapterPosition()).getClasses_name()));
alertDialogueBuilder.setCancelable(false).setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// long id = classes.get(getAdapterPosition()).getId();
String classesBean = editText.getText().toString();
try {
/**This is as a result of trying to find a solution**/
//long classesArraySize = classes.size();
// while (classes.get(getAdapterPosition()).getId() < classesArraySize)
/**Source of error**/
((ClassListOnSetup) context).editClassnameAt(classes.get(getAdapterPosition()).getId(), getAdapterPosition(), classesBean);
} catch (ArrayIndexOutOfBoundsException exception){
Log.i("Errore", String.valueOf(exception));
}
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = alertDialogueBuilder.create();
alert.show();
}
});
}
}
public ClassListOnSetupRecyclerAdapter(Context context, List<ClassesBean> classes){
mContext = context;
listClasses = classes;
}
@Override
public ClassListOnSetupRecyclerAdapter.ClassListOnSetupViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_classesname_recycler,parent,false);
return new ClassListOnSetupViewHolder(mContext,itemView,listClasses);
}
@Override
public void onBindViewHolder(ClassListOnSetupViewHolder holder, final int position) {
holder.textView_classname.setText(listClasses.get(position).getClasses_name());
}
@Override
public int getItemCount() {
Log.v(ClassListOnSetupRecyclerAdapter.class.getSimpleName(),"ListClasses"+listClasses.size());
return listClasses.size();
}
}
我的JavaBean类名为ClassesBean
public class ClassesBean {
private Long id;
private int classes_item_index;
private String classes_name;
private String classes_codename;
private String classes_sections;
private String classes_teachers;
private String classes_students;
public int getClasses_item_index() {
return classes_item_index;
}
public void setClasses_item_index(int classes_item_index) {
this.classes_item_index = classes_item_index;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getClasses_name() {
return classes_name;
}
public void setClasses_name(String classes_name) {
this.classes_name = classes_name;
}
public String getClasses_codename() {
return classes_codename;
}
public void setClasses_codename(String classes_codename) {
this.classes_codename = classes_codename;
}
public String getClasses_sections() {
return classes_sections;
}
public void setClasses_sections(String classes_sections) {
this.classes_sections = classes_sections;
}
public String getClasses_teachers() {
return classes_teachers;
}
public void setClasses_teachers(String classes_teachers) {
this.classes_teachers = classes_teachers;
}
public String getClasses_students() {
return classes_students;
}
public void setClasses_students(String classes_students) {
this.classes_students = classes_students;
}
}
答案 0 :(得分:1)
不应在视图持有者的构造函数中附加侦听器,而应将其附加到onBindViewHolder()
并使用提供的位置而不是调用getAdapterPosition()
。
视图持有者构造函数应仅用于初始化视图:
public class ClassListOnSetupViewHolder extends RecyclerView.ViewHolder {
public TextView textView_classname;
public Button delete_button_classsetup;
public ClassListOnSetupViewHolder(final Context context, View itemView, final List<ClassesBean> classes) {
super(itemView);
textView_classname = itemView.findViewById(R.id.textView_classname);
delete_button_classsetup = itemView.findViewById(R.id.delete_button_classsetup);
}
}
答案 1 :(得分:0)
getAdapterPosition()返回-1。这意味着Adapter获取NO_POSITION,并且它总是在调用notifyDataSetChanged()时发生。因此,您可以编辑getAdapterPosition()的日志。同时,您应该确保getAdapterPosition ()!= -1或getAdapterPosition()在调用list.get(index)之前列表的索引。顺便说一句,我们总是在onBindViewHolder()而不是ViewHolder的构造函数中设置click侦听器。