房间数据库中的数据不会显示在“回收者”视图中。可能是什么问题?

时间:2019-04-18 14:12:43

标签: android mvvm kotlin android-recyclerview android-room

我想通过RoomDatabase在回收者视图中显示我的物品。但是,当我启动应用程序时-它显示只有标题的空白屏幕。

这是用于回收站视图的适配器

class AlarmAdapter : ListAdapter<Alarm, AlarmAdapter.AlarmHolder>(DIFF_CALLBACK) {
    private lateinit var alarmViewModel: AlarmViewModel
    companion object {
        private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Alarm>() {
            override fun areItemsTheSame(oldItem: Alarm, newItem: Alarm): Boolean {
                return oldItem.id == newItem.id
            }
            override fun areContentsTheSame(oldItem: Alarm, newItem: Alarm): Boolean {
                return oldItem.days == newItem.days && oldItem.timeInMinutes == newItem.timeInMinutes
                        && oldItem.departTimeInMinutes == newItem.departTimeInMinutes && oldItem.fromAddress == newItem.fromAddress
                        && oldItem.toAddress == newItem.toAddress && oldItem.isEnabled == newItem.isEnabled
            }
        }
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlarmHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.alarm_item, parent, false)
        alarmViewModel = ViewModelProviders.of(parent.context as FragmentActivity).get(AlarmViewModel::class.java)
        return AlarmHolder(itemView)
    }
    override fun onBindViewHolder(holder: AlarmHolder, position: Int) {
        val currentAlarm = getItem(position)
        holder.fromAddress.text = currentAlarm.fromAddress
        // and other texts
        holder.alarmSwitch.isEnabled = currentAlarm.isEnabled
        holder.alarmSwitch.setOnClickListener {
            holder.alarmSwitch.toggle()
            currentAlarm.isEnabled = !currentAlarm.isEnabled
            alarmViewModel.update(currentAlarm)
        }
    }
    inner class AlarmHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        internal var fromAddress = itemView.findViewById<TextView>(R.id.alarm_item_from_address)
        // and other resources
    }
}

这是MainActivity的一部分

class MainActivity : AppCompatActivity() {
    companion object {
        private const val DAY_MINUTES = 24 * 60
    }
    private var currentEditAlarmDialog: EditAlarmDialog? = null
    private lateinit var alarmViewModel: AlarmViewModel
    private lateinit var adapter: AlarmAdapter
    private lateinit var layoutManager: LinearLayoutManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        delegate.localNightMode = AppCompatDelegate.MODE_NIGHT_YES
        setContentView(R.layout.activity_main)
        layoutManager = LinearLayoutManager(this)
        recycler_view.layoutManager = layoutManager
        recycler_view.setHasFixedSize(true)
        adapter = AlarmAdapter()
        recycler_view.adapter = adapter
        alarmViewModel = ViewModelProviders.of(this).get(AlarmViewModel::class.java)
        alarmViewModel.getAllAlarms().observe(this, Observer<List<Alarm>> {
            adapter.submitList(it)
        })
    }

AlarmViewModel类

class AlarmViewModel(application: Application) : AndroidViewModel(application) {
    private val repository = AlarmRepository(application)
    private val allAlarms = repository.getAllAlarms()

    fun insert(alarm: Alarm) {
        repository.insert(alarm)
    }

    fun update(alarm: Alarm) {
        repository.update(alarm)
    }

    fun delete(alarm: Alarm) {
        repository.delete(alarm)
    }

    fun deleteAllAlarms() {
        repository.deleteAllNotes()
    }

    fun getAllAlarms() = allAlarms

    fun getAlarmById(id: Int) = repository.getAlarmById(id)
}

这是一个存储库

class AlarmRepository(application: Application) {
    private var alarmDao: AlarmDao
    private var allAlarms: LiveData<List<Alarm>>

    private val uiScope = CoroutineScope(Dispatchers.IO)

    init {
        val database: AlarmDatabase = AlarmDatabase.getInstance(application.applicationContext)!!
        alarmDao = database.alarmDao()
        allAlarms = alarmDao.getAllAlarms()
    }

    fun getAllAlarms() = allAlarms

    fun insert(alarm: Alarm) {
        uiScope.launch { alarmDao.insert(alarm) }
    }

    fun delete(alarm: Alarm) {
        uiScope.launch { alarmDao.delete(alarm) }
    }

    fun deleteAllNotes() {
        uiScope.launch { alarmDao.deleteAllAlarms() }
    }

    fun update(alarm: Alarm) {
        uiScope.launch { alarmDao.update(alarm) }
    }

    fun getAlarmById(id: Int) = alarmDao.getAlarmById(id)
}

还有道课

@Dao
interface AlarmDao {

    @Insert
    fun insert(alarm: Alarm)

    @Delete
    fun delete(alarm: Alarm)

    @Update
    fun update(alarm: Alarm)

    @Query("DELETE FROM alarm_table")
    fun deleteAllAlarms()

    @Query("SELECT * FROM alarm_table ORDER BY days ASC")
    fun getAllAlarms() : LiveData<List<Alarm>>

    @Query("SELECT * FROM alarm_table WHERE id = :id")
    fun getAlarmById(id: Int) : Alarm
}

我该怎么做才能解决此问题? 如果您需要更多代码,请告诉我!

1 个答案:

答案 0 :(得分:0)

在AlarmViewModel类中,将对调用的调用放入getAllAlarms方法中。

fun getAllAlarms() {
   repository.getAllAlarms()
}

从init方法中删除

allAlarms = alarmDao.getAllAlarms()

放入

fun getAllAlarms() = alarmDao.getAllAlarms()