Dao class 的实例未正确初始化
instance of Dao class is not being initialized correctly
我想使用 Kotlin Coroutine
将数据库中的数据加载到 RecyclerView 中,但它不会在第一次启动时加载数据,但当应用程序重新启动时它会正确加载数据。存储库 class:
中似乎有问题
NoteDao.kt 是:
@Dao
interface NoteDAO {
@Query("SELECT * FROM NoteTable ORDER BY noteDate DESC")
fun findAllNotes(): List<Note>
}
NoteRepository.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 是
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 class 并将 init
块放入:
init {
CoroutineScope(Dispatchers.IO).launch {
val noteData = noteDAO.findAllNotes()
noteData.collect{ // it is related to Flow API
allNotes.postValue(it)
}
}
}
return allNotes
getAllNotes()
fun getAllNotes(): MutableLiveData<List<Note>> {
return allNotes
}
现在你必须检查它是否有效?
我不同意已接受的答案。 CoroutineScope
.
不是问题
原始代码返回空 List
的原因是因为您没有将 DAO 函数声明为 suspend
函数。由于 getAllNotes()
不是 suspend
函数,它不会暂停调用协程,直到 Room returns 得到结果,因此 returns 是一个空列表。
如果您要将 Flow
转换为 LiveData
,我也建议使用 asLiveData()
。
我想使用 Kotlin Coroutine
将数据库中的数据加载到 RecyclerView 中,但它不会在第一次启动时加载数据,但当应用程序重新启动时它会正确加载数据。存储库 class:
NoteDao.kt 是:
@Dao
interface NoteDAO {
@Query("SELECT * FROM NoteTable ORDER BY noteDate DESC")
fun findAllNotes(): List<Note>
}
NoteRepository.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 是
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 class 并将 init
块放入:
init {
CoroutineScope(Dispatchers.IO).launch {
val noteData = noteDAO.findAllNotes()
noteData.collect{ // it is related to Flow API
allNotes.postValue(it)
}
}
}
return allNotes
getAllNotes()
fun getAllNotes(): MutableLiveData<List<Note>> {
return allNotes
}
现在你必须检查它是否有效?
我不同意已接受的答案。 CoroutineScope
.
原始代码返回空 List
的原因是因为您没有将 DAO 函数声明为 suspend
函数。由于 getAllNotes()
不是 suspend
函数,它不会暂停调用协程,直到 Room returns 得到结果,因此 returns 是一个空列表。
如果您要将 Flow
转换为 LiveData
,我也建议使用 asLiveData()
。