gpt4 book ai didi

java - 从SQLite数据库删除项目时出错

转载 作者:行者123 更新时间:2023-12-02 03:59:58 24 4
gpt4 key购买 nike

我正在尝试从SQLite数据库中删除RecyclerView中的项目,但出现以下错误:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at com.reogen.bettarium.Fragments.ListFragment.onSwipe(ListFragment.java:135)
at com.reogen.bettarium.Helpers.SwipeToDelete.onSwiped(SwipeToDelete.java:30)


但是我不明白怎么了。这两行是:

            public Void onSwipe(RecyclerView.ViewHolder viewHolder, int direction, int position) {
if (viewHolder instanceof ToDoAdapter.ToDoViewHolder) {
String name = list.get(viewHolder.getAdapterPosition()).get_title();<-----




public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

if (listener != null) {
listener.onSwipe(viewHolder, direction, viewHolder.getAdapterPosition()); <-----

}

}


为什么我的应用崩溃了并且没有从数据库中删除项目?
非常感谢

编辑:
我正在尝试使用主要片段中的OnSwipe从recyclerView和数据库中删除项目:

 public Void onSwipe(RecyclerView.ViewHolder viewHolder, int direction, int position) {
if (viewHolder instanceof ToDoAdapter.ToDoViewHolder) {
String name = list.get(viewHolder.getAdapterPosition()).get_title();
int deleteIndex = viewHolder.getAdapterPosition();

toDoAdapter.removeItem(deleteIndex);

mDatabase.execSQL("DELETE FROM " + TABLE_TODO + " WHERE " + COLUMN_TODONAME + "=\"" + name + "\";");


//mDatabase.delete(ToDoDBHandler.ToDoItemDatabase.TABLE_TODO, ToDoDBHandler.ToDoItemDatabase.COLUMN_ID + "=" + position, null);
toDoAdapter.swapCursor(newCursor());
}

最佳答案

尝试将String name = list.get(viewHolder.getAdapterPosition()).get_title();更改为String name = list.get(position).get_title();

关于java - 从SQLite数据库删除项目时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56739048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com