I have a horizontal view of the recycler with custom elements in it. Each element can hold the position of the current element in the Recycler view. I want to update the position of a position when an element is moved using drag and drop. However, data is deleted when there are more than three items in the horizontal view. Please help me.
Source
This is what I get in Logcat:
E / ROOM: The invalidation alarm is initialized twice: /.
E / Moved: Counterfrom3
next item: to2
Initializing a 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();
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;
}
Code to update data when moving items.
public static void MoveItem(int fromPosition,int toPosition){
String name = data.get(fromPosition).getName();
String nexName = data.get(toPosition).getName();
ContentValues fromContentValues = new ContentValues();
fromContentValues.put("posItem", toPosition);
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();
} finally {
db.endTransaction();
db.close();
}
}