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
This is what i am getting in Logcat:
E/ROOM: Invalidation tracker is initialized twice :/.
next item:to2
Initialization of database in onCreate.
db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, DB_NAME) .fallbackToDestructiveMigration() .allowMainThreadQueries() .build();
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 } }