如何通过Room和LiveData保留RecyclerView顺序中的更改?

时间:2019-07-02 10:49:11

标签: android kotlin android-room android-livedata itemtouchhelper

在确定RecyclerView项目顺序更改后,如何确定如何更新会议室数据库中的数据时遇到问题。我应该如何根据用户操作来更新Room中的LiveData项目?

使用ItemTouchHelper.Callback,我设置了一个onMove回调,它可以更改呈现给用户的项目顺序(在拖放时),但是当我打电话以更新Room中的项目顺序时数据库使用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数据库?

1 个答案:

答案 0 :(得分:0)

您可以按照this关于Udacity的指南进行操作。它是免费的,由Google制造,并使用Kotlin。