我具有带有自定义项目的水平回收视图。每个项目都可以在回收器视图中保存当前项目的位置。使用拖放移动项目时,我想更新项目位置。但是,当水平视图中有三个项目时,数据将被删除。请帮助我。源代码
这就是我在logcat中得到的:
e/房间:无效跟踪器初始化了两次:/。
e/item移动:counterfrom3
下一个项目:TO2
ongreate中数据库的初始化。
db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, DB_NAME)
.fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build();
recyclerview适配器代码。
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
String name = dataSet.get(fromPosition).getName();
//this will make "Add item" do not move from its first position..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (!(Objects.equals(name, "Add") || (toPosition == 0 && fromPosition == 1))) {
Collections.swap(dataSet, fromPosition, toPosition);
MoveItem(fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
return true;
}
}
return false;
}
移动项目时更新数据的代码。
public static void MoveItem(int fromPosition,int toPosition){
String name = data.get(fromPosition).getName(); //This gets the current item name in the view
String nexName = data.get(toPosition).getName(); //This gets the next item name in the view
ContentValues fromContentValues = new ContentValues();
fromContentValues.put("posItem", toPosition); //adding data to ContentValues
ContentValues toContentValues = new ContentValues();
toContentValues.put("posItem", fromPosition);
Log.e("Item moved", name + "from" + fromPosition + "n" + "next item:" + "to" + toPosition);
db.beginTransaction();
try {
db.getOpenHelper().getWritableDatabase().update(name,
0, fromContentValues, "posItem =" + fromPosition, null);
db.getOpenHelper().getWritableDatabase().update(nexName,
0, toContentValues, "posItem =" + toPosition, null);
db.setTransactionSuccessful(); //setting Transaction Successful
} finally {
db.endTransaction(); // commit or rollback
db.close(); //closing database
}
}
当我迁移数据库版本时,同样的错误发生了E/ROOM: Invalidation tracker is initialized twice
,杀死应用程序并重新打开工作。当我开始使用v1.1.0的房间时。
但是,如果我保持所有相同的东西,然后使用v1.0.0房间返回,则不会发生这种问题,并且一切正常。
so,可能是房间v1.1.0问题
Google问题