正在从文件室数据库中删除问题



我在从应用程序中删除项目时遇到问题。它实际上是一个笔记应用程序,我在其中创建了一个删除笔记的AlertDialog函数。这是我的功能的代码

private void showDeleteDialog(int position) {
AlertDialog.Builder alert = new AlertDialog.Builder(context)
.setTitle("Delete view")
.setMessage("Are you sure to delete")
.setIcon(R.drawable.ic_baseline_delete_24)
.setPositiveButton("yes", (dialogInterface, i) -> databaseHelper.notesDao().deleteNotes(new Notes(arrNotes.get(position).id, arrNotes.get(position).title, arrNotes.get(position).text)))
.setNegativeButton("No", (dialogInterface, i) -> {
});
alert.show();
}

这里我称之为

holder.llView.setOnLongClickListener(view -> {
showDeleteDialog(position);
return true;
});

当我点击"是"时,应用程序崩溃了,但它成功地从数据库中删除了笔记。这就是我得到的错误

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{a4d7d56 position=1 id=-1, oldPos=0, pLpos:0 scrap [attachedScrap] tmpDetached no parent} androidx.recyclerview.widget.RecyclerView{815f028 VFED..... ......I. 31,171-689,776 #7f090165 app:id/recycler_view}, adapter:com.example.keepnotes.RecyclerViewAdapter@773a141, layout:androidx.recyclerview.widget.StaggeredGridLayoutManager@a3a6e6, context:com.example.keepnotes.MainActivity@3065e55

所有这些都在RecyclerView适配器中。recyclerView适配器的代码如下

package com.example.keepnotes;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
ArrayList<Notes> arrNotes;
DatabaseHelper databaseHelper;
private RecyclerView.RecyclerListener listener;
RecyclerViewAdapter(Context context, ArrayList<Notes> arrNotes, DatabaseHelper databaseHelper) {
this.context = context;
this.arrNotes = arrNotes;
this.databaseHelper = databaseHelper;
}
public RecyclerViewAdapter(ArrayList<Notes> arrNotes, RecyclerView.RecyclerListener listener) {
this.arrNotes = arrNotes;
this.listener = listener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.single_view, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
holder.title.setText(arrNotes.get(position).title);
holder.body.setText(arrNotes.get(position).text);
holder.index.setText(String.valueOf(position + 1));

holder.llView.setOnClickListener(view -> {
Intent iNext = new Intent(context, ViewActivity.class);
iNext.putExtra("title", arrNotes.get(position).title);
iNext.putExtra("text", arrNotes.get(position).text);
context.startActivity(iNext);
});
holder.llView.setOnLongClickListener(view -> {
showDeleteDialog(position);
return true;
});

}
@Override
public int getItemCount() {
return arrNotes.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title, body, index;
CardView llView;
public ViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.text_title_view);
body = itemView.findViewById(R.id.text_text_view);
index = itemView.findViewById(R.id.index);
llView = itemView.findViewById(R.id.card_View);
databaseHelper = DatabaseHelper.getDatabase(context);
}
}
private void showDeleteDialog(int position) {
AlertDialog.Builder alert = new AlertDialog.Builder(context)
.setTitle("Delete view")
.setMessage("Are you sure to delete")
.setIcon(R.drawable.ic_baseline_delete_24)
.setPositiveButton("yes", (dialogInterface, i) -> databaseHelper.notesDao().deleteNotes(new Notes(arrNotes.get(position).id, arrNotes.get(position).title, arrNotes.get(position).text)))
.setNegativeButton("No", (dialogInterface, i) -> {
});
alert.show();
}
}

我怎样才能摆脱这个错误????

它已经解决了,事实上,在Live Data中,我使用了notifyiteminserted,我将其更改为notifyDataSetChnaged。

相关内容

  • 没有找到相关文章