Я хотел бы показать свои предметы в виде переработчика через 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
}
Что я могу сделать, чтобы решить эту проблему?
Если вам нужно больше кода - просто скажите мне об этом!