android - Dao类的实例未正确初始化

标签 android kotlin kotlin-coroutines android-viewmodel android-mvvm

我想使用Kotlin Coroutine从数据库中将数据加载到RecyclerView中,但是在第一次启动时它不会加载数据,但是当App重新启动时,它会正确加载数据。似乎存储库类有问题:

NoteDao.kt是:

@Dao
interface NoteDAO {

    @Query("SELECT * FROM NoteTable ORDER BY noteDate DESC")
    fun findAllNotes(): List<Note>

}

注意Repository.kt
class NoteRepository(application: Application) {

    private var allNotes = MutableLiveData<List<Note>>()

    private val noteDAO = NoteDatabase.getDatabase(application).getNoteDao() // Issue is caused by this line because data is not assigned to noteDAO 

    fun getAllNotes(): MutableLiveData<List<Note>> {
        CoroutineScope(Dispatchers.IO).launch {
            val noteData = noteDAO.findAllNotes()
            allNotes.postValue(NoteData)
        }
        return allNotes   // it also returns empty List with size = 0
    }

}

适配器类为
class NoteAdapter(val context: Context): RecyclerView.Adapter<NoteAdapter.NoteHolder>() {

    private var allNotes: List<Note> = ArrayList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.Note_item, parent, false)
        return NoteHolder(itemView)
    }

    override fun onBindViewHolder(holder: NoteHolder, position: Int) {
        val currentNote = allNotes[position]
        holder.title.text = currentNote.title
        holder.date.text = DateUtils.getDate(Date(currentNote.date))

    }

    override fun getItemCount(): Int {
        return allNotes.size
    }

    fun addNotes(allNotes: List<Note>) {
        this.allNotes = allNotes
        notifyDataSetChanged()
    }

    inner class NoteHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var title: LabelTextView = itemView.findViewById(R.id.item_title)
        var date: LabelTextView = itemView.findViewById(R.id.item_date)
    }

}

并且ViewModel是
class NoteViewModel(
    application: Application
): AndroidViewModel(application) {

    private var repository = NoteRepository(application)

    private var _allNotes = repository.getAllNotes()

    val allNotes: LiveData<List<Note>>
        get() = _allNotes
}

最后将它附加在MainActivity中
class MainActivity : AppCompatActivity(){

    private lateinit var mNoteViewModel: NoteViewModel
    private lateinit var mNoteAdapter: NoteAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)    

        mNoteAdapter = NoteAdapter(this)

        mNoteViewModel = ViewModelProviders.of(this).get(NoteViewModel::class.java)
        mNoteViewModel.allNotes.observe(
            this, Observer {
                Log.e("MAIN_ACTIVITY", "Called...") // it is printed in Log but data is not recieved from repository
                mNoteAdapter.addNotes(it)
            }
        )

        rv_log.layoutManager = GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false)

        mNoteAdapter.notifyDataSetChanged()
        rv_log.adapter = mNoteAdapter
    }

}

所有人都没有在RecyclerView中加载和显示数据。

最佳答案

看来问题出在getAllNotes()上,这意味着CoroutineScope无法正常工作。您应该使用Flow API并将Dao方法更改为

@Query("SELECT * FROM NoteTable ORDER BY noteDate DESC")
fun findAllNotes(): Flow<List<Note>>

之后,您必须更改NoteRepository类,并将init块设置为:
init {
    CoroutineScope(Dispatchers.IO).launch {
         val noteData = noteDAO.findAllNotes()
         noteData.collect{ // it is related to Flow API
             allNotes.postValue(it)
         }
    }
} 

然后只用allNotes返回getAllNotes()
fun getAllNotes(): MutableLiveData<List<Note>> {            
    return allNotes 
}

现在您必须检查它是否有效?

关于android - Dao类的实例未正确初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59944435/

相关文章:

android - 可扩展 ListView 在 jellyBean 4.3 版本中向右移动组图标指示器?

java - 菜单操作项的 setVisibility

android - 未捕获的翻译错误 : com. android.dx.cf.code.SimException: local 0007: invalid

android - 协程没有启动?

kotlin - 如何从不同的函数发出 Flow 值? Kotlin 协程

kotlin - Kotlin Coroutine Flow api是否具有Rx Subject等桥接工具?

java - 如何使用ViewPager android禁用或隐藏键盘的左右箭头

java - 如何在android中的日历上的特定日期设置点

kotlin - 使用类字段的Kotlin Data类 setter

java - 从 JSONArray 获取元素时保留双引号