在 RecyclerView 项目顺序发生更改后,我无法弄清楚我应该如何更新房间数据库中的数据。我应该如何根据用户操作更新 Room 中的 LiveData 项目?
使用 ItemTouchHelper.Callback 我设置了一个 onMove 回调,可以更改呈现给用户的项目顺序(拖放时),但是当我调用更新房间数据库中项目的顺序时,使用ViewModel 对象,然后用户一次只能移动一个项目。所以如果你拖动一个项目,它只会移动一个空格。
这是我在 ListAdapter 中定义的 onMove 函数,它为回调实现了 ItemTouchHelperAdapter。
override fun onMove(
recyclerView: RecyclerView,
fromViewHolder: RecyclerView.ViewHolder,
toViewHolder: RecyclerView.ViewHolder
): Boolean {
d(this.TAG, "swap viewHolders: " + fromViewHolder.adapterPosition + " to " + toViewHolder.adapterPosition)
val workoutRoutine1 = workoutRoutines[fromViewHolder.adapterPosition]
val workoutRoutine2 = workoutRoutines[toViewHolder.adapterPosition]
workoutRoutine1.orderNumber = toViewHolder.adapterPosition.toLong()
workoutRoutine2.orderNumber = fromViewHolder.adapterPosition.toLong()
//this.workoutRoutinesViewModel.update(workoutRoutine1)
//this.workoutRoutinesViewModel.update(workoutRoutine2)
notifyItemMoved(fromViewHolder.adapterPosition, toViewHolder.adapterPosition)
return true
}
这是我的 DAO 对象
@Dao
interface WorkoutRoutineDAO {
@Insert
suspend fun insert(workoutRoutine: WorkoutRoutine)
@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun update(workoutRoutine: WorkoutRoutine)
@Delete
suspend fun delete(workoutRoutine: WorkoutRoutine)
@Query("DELETE FROM workout_routine_table")
fun deleteAll()
@Query("SELECT * FROM workout_routine_table ORDER BY order_number ASC")
fun getAllWorkoutRoutines(): LiveData<List<WorkoutRoutine>>
@Query("SELECT COALESCE(MAX(order_number), -1) FROM workout_routine_table")
fun getLargestOrderNumber(): Long
}
这是我的 RoomDatabase 对象
@Database(entities = [WorkoutRoutine::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun workoutRoutineDAO(): WorkoutRoutineDAO
companion object {
@Volatile
private var INSTANCE: AppDatabase? = null
fun getDatabase(
context: Context,
scope: CoroutineScope
): AppDatabase {
return INSTANCE ?: synchronized(this) {
val instance =
Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, "app_database")
.addCallback(WorkoutRoutineDatabaseCallback(scope))
.build()
INSTANCE = instance
instance
}
}
}
private class WorkoutRoutineDatabaseCallback(
private val scope: CoroutineScope
) : RoomDatabase.Callback() {
}
}
这是我实现的 ViewModel 对象。
class WorkoutRoutinesViewModel(application: Application) : AndroidViewModel(application) {
private val workoutRoutinesRepository: WorkoutRoutineRepository
val allWorkoutRoutines: LiveData<List<WorkoutRoutine>>
init {
// Get the DAO
val workoutRoutineDAO = AppDatabase.getDatabase(application, viewModelScope).workoutRoutineDAO()
// Build a new data repository for workout routines
workoutRoutinesRepository = WorkoutRoutineRepository(workoutRoutineDAO)
// Get a live view of the workout routines database
allWorkoutRoutines = workoutRoutinesRepository.allRoutines
}
fun insert(workoutRoutine: WorkoutRoutine) = viewModelScope.launch(Dispatchers.IO) {
workoutRoutinesRepository.insert(workoutRoutine)
}
fun update(workoutRoutine: WorkoutRoutine) = viewModelScope.launch(Dispatchers.IO) {
workoutRoutinesRepository.update(workoutRoutine)
}
fun delete(workoutRoutine: WorkoutRoutine) = viewModelScope.launch(Dispatchers.IO) {
workoutRoutinesRepository.delete(workoutRoutine)
}
}
我希望用户能够移动项目 n 个空格,然后删除,并在用户删除项目时执行对 Room 数据库的更新,但是如果我将 Room 更新放在 onMove 方法中,用户只能移动项目一次。
当对象的顺序在回收站视图中发生变化时,我试图了解更新房间数据的正确方法。我试图让这些对象的顺序保持不变,即使用户退出应用程序或更改活动等。我应该如何使用 LiveData 将这些更改回显到 Room 数据库?