我有一个横向回收与定制物品在它的看法。每个项目都可以在“回收器”视图中保持当前项的位置。使用拖放移动项目时,我希望更新项目位置。但是,当水平view.Please中有三个以上的项时,数据将被删除。源代码
这就是我在“逻辑猫”中得到的:
E/ROOM:无效跟踪器初始化两次:/。 E/项目移动: Counterfrom3 下一项:to2 2
在onCreate中初始化数据库。
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
}
}发布于 2018-05-29 07:00:12
当我迁移数据库版本时,同样的错误会发生在E/ROOM: Invalidation tracker is initialized twice,关闭应用程序,然后重新打开工作。当我开始使用OfficeV1.1.0的时候。
但是,如果我保持一切不变,回到使用Room v1.0.0,就不会出现这样的问题,而且一切都很完美。
所以,可能是v1.1.0版
https://stackoverflow.com/questions/47691422
复制相似问题