我想使用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
}
现在您必须检查它是否有效?