Room Invalidation跟踪器初始化了两次 [英] Room Invalidation tracker is initialized twice
问题描述
我有一个水平的回收站视图,其中包含自定义项目.每个项目都可以在回收者"视图中保留当前项目的位置.我想在使用拖放功能移动项目时更新项目位置.但是,当水平视图中有三项以上时,数据将被删除.请帮帮我. 源代码
I have a horizontal recycler view with custom items in it . Each item can hold the position of current item in the Recycler view . I want to update the item position when item is moved using drag and drop . However the data is getting deleted when there are more then three items in horizontal view.Please Help me out . Source Code
这就是我在Logcat中得到的:
This is what i am getting in Logcat:
E/ROOM:无效跟踪器被初始化两次:/.
E/ROOM: Invalidation tracker is initialized twice :/.
E/物品已移动:Counterfrom3
E/Item moved: Counterfrom3
下一项:to2
在onCreate中初始化数据库.
Initialization of database in onCreate.
db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, DB_NAME)
.fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build();
RecyclerView适配器代码.
RecyclerView Adapter code.
@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;
}
移动项目时更新数据的代码.
The code to update the data when items are moved.
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
,杀死该应用程序,然后重新打开工作.当我开始使用Room v1.1.0时.
When I migrate the database version, same error happens E/ROOM: Invalidation tracker is initialized twice
, kills the app, and reopens work. When I started using Room v1.1.0.
但是,如果我将所有内容保持不变并重新使用Room v1.0.0,则不会发生此类问题,并且一切正常.
But if I keep everything the same and go back to using Room v1.0.0, no such problem occurs and everything works perfectly.
所以,可能是Room v1.1.0问题
So, May be Room v1.1.0 issue
这篇关于Room Invalidation跟踪器初始化了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!