Я хочу удалить элементы с помощью диалогового окна оповещения. В то же время, когда я нажимаю «да», мое приложение вылетает, но в то же время элемент удаляется, чего я и хочу, за исключением cra sh.
Ниже я вставил свой RecycleAdapter.
Вот мой код:
public RecycleAdapter(ArrayList<RetrieveRunnerActivity> arrayList, Context context, Cursor cursor, OnItemClickListener deleteListener) {
this.arrayList = arrayList;
this.context = context;
this.cursor = cursor;
this.deleteListener = deleteListener;
}
private OnItemClickListener deleteListener;
public interface OnItemClickListener {
void onDeleteClick(int position);
}
public void setItemListener(OnItemClickListener listener) {
deleteListener = listener;
}
public RecycleAdapter(ArrayList<RetrieveRunnerActivity> arrayList) {
this.arrayList = arrayList;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.history, parent, false);
RecyclerViewHolder RVH = new RecyclerViewHolder(view, deleteListener, context, arrayList);
return RVH;
}
@Override
public void onBindViewHolder(RecycleAdapter.RecyclerViewHolder holder, int position) {
RetrieveRunnerActivity retrieveRunnerActivity = arrayList.get(position);
holder.Duration_column.setText(String.valueOf(retrieveRunnerActivity.getElapsed_time()));
holder.Distance_column.setText(String.valueOf(retrieveRunnerActivity.getTotal_distance()));
holder.date_heading.setText(String.valueOf(retrieveRunnerActivity.getDate()));
holder.Entry_column.setText(String.valueOf(retrieveRunnerActivity.getId()));
holder.deleteButton.getContext();
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
ImageView deleteButton;
TextView Duration_column, Distance_column, date_heading, Entry_column;
RunoraDatabaseHelper Runora_database;
RecyclerViewHolder(final View view, final OnItemClickListener listener, final Context context, final ArrayList arrayList) {
super(view);
Duration_column = view.findViewById(R.id.Duration_column);
Distance_column = view.findViewById(R.id.Distance_column);
date_heading = view.findViewById(R.id.date_heading);
Entry_column = view.findViewById(R.id.Entry_column);
deleteButton = view.findViewById(R.id.deleteButton);
deleteButton.setTag(deleteListener);
final RecycleAdapter adapter = new RecycleAdapter(arrayList);
Runora_database = new RunoraDatabaseHelper(context);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(context);
dialogbuilder.setMessage("Delete activity permanently")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int
position) {
Runora_database.DeleteData(Entry_column.getText().toString());
listener.onDeleteClick(position);
adapter.notifyDataSetChanged();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = dialogbuilder.create();
alertDialog.show();
Вот ошибка:
java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
at java.util.ArrayList.remove(ArrayList.java:405)
at mobile.apps.DisplayHistory$5.deleteItems(DisplayHistory.java:189)
at mobile.apps.DisplayHistory$5.onDeleteClick(DisplayHistory.java:182)
at mobile.RecycleAdapter$RecyclerViewHolder$1$2.onClick(RecycleAdapter.java:122)
Я застрял на этом некоторое время. Любая помощь приветствуется.