无法将暂停功能添加到我的 AlertDialog 中的 setOnClickListener
Can't add a suspended function to a setOnClickListener in my AlertDialog
songToAddDialog() 方法中的 add_button 不会将我暂停的方法 positiveButtonClick() 接受到其 setOnClickListener 中。我已经看了好几个小时了,但我不知道该怎么做。
// Check the GenreKey of the current playlist to use later to create the Song
suspend fun playlistGenreCheck(id: Long): Long {
val playlist = dataSource.getPlaylist(id)
val playlistGenre = playlist.playlistGenre
return playlistGenre
}
// When you press the add button in the AlertDialog it will add the song to the database and closes the AlertDialog afterwards
suspend fun positiveButtonClick(builder: AlertDialog){
val song = Song(title = builder.dialogTitleEt.toString(), artist = builder.dialogArtistEt.toString(), playlist = arguments.playlistKey, key = builder.dialogKeyEt.toString(), genre = playlistGenreCheck(arguments.playlistKey))
songsModel.addSong(song)
builder.dismiss()
}
// When you press the cancel button the AlertDialog will bet dismissed
fun negativeButtonClick(builder: AlertDialog){
builder.dismiss()
}
// This creates the AlertDialog and adds the two functions above to the buttons
fun songToAddDialog(){
val mDialogView = LayoutInflater.from(requireContext()).inflate(R.layout.add_song_dialog, null)
val mBuilder = AlertDialog.Builder(requireContext()).setView(mDialogView).setTitle("Add a Song")
val mAlertDialog = mBuilder.show()
mDialogView.add_button.setOnClickListener{positiveButtonClick(mAlertDialog)}
mDialogView.cancel_button.setOnClickListener{negativeButtonClick(mAlertDialog)}
}
// Makes the add-button inside the songslistview observable
songsModel.addButton.observe(viewLifecycleOwner, androidx.lifecycle.Observer{
if (it) {
songToAddDialog()
}
})
Suspend
函数只能从 CoroutineScope
调用。如果你有 lifecycle
依赖然后使用:
mDialogView.add_button.setOnClickListener{
lifecyclescope.launch{
positiveButtonClick(mAlertDialog)
}
}
如果您没有生命周期依赖项,那么像这样调用 CoroutineScope
也应该有效:
mDialogView.add_button.setOnClickListener{
CoroutineScope(Dispatchers.IO).launch{
positiveButtonClick(mAlertDialog)
}
}
如果您对此仍有问题,请告诉我:)
songToAddDialog() 方法中的 add_button 不会将我暂停的方法 positiveButtonClick() 接受到其 setOnClickListener 中。我已经看了好几个小时了,但我不知道该怎么做。
// Check the GenreKey of the current playlist to use later to create the Song
suspend fun playlistGenreCheck(id: Long): Long {
val playlist = dataSource.getPlaylist(id)
val playlistGenre = playlist.playlistGenre
return playlistGenre
}
// When you press the add button in the AlertDialog it will add the song to the database and closes the AlertDialog afterwards
suspend fun positiveButtonClick(builder: AlertDialog){
val song = Song(title = builder.dialogTitleEt.toString(), artist = builder.dialogArtistEt.toString(), playlist = arguments.playlistKey, key = builder.dialogKeyEt.toString(), genre = playlistGenreCheck(arguments.playlistKey))
songsModel.addSong(song)
builder.dismiss()
}
// When you press the cancel button the AlertDialog will bet dismissed
fun negativeButtonClick(builder: AlertDialog){
builder.dismiss()
}
// This creates the AlertDialog and adds the two functions above to the buttons
fun songToAddDialog(){
val mDialogView = LayoutInflater.from(requireContext()).inflate(R.layout.add_song_dialog, null)
val mBuilder = AlertDialog.Builder(requireContext()).setView(mDialogView).setTitle("Add a Song")
val mAlertDialog = mBuilder.show()
mDialogView.add_button.setOnClickListener{positiveButtonClick(mAlertDialog)}
mDialogView.cancel_button.setOnClickListener{negativeButtonClick(mAlertDialog)}
}
// Makes the add-button inside the songslistview observable
songsModel.addButton.observe(viewLifecycleOwner, androidx.lifecycle.Observer{
if (it) {
songToAddDialog()
}
})
Suspend
函数只能从 CoroutineScope
调用。如果你有 lifecycle
依赖然后使用:
mDialogView.add_button.setOnClickListener{
lifecyclescope.launch{
positiveButtonClick(mAlertDialog)
}
}
如果您没有生命周期依赖项,那么像这样调用 CoroutineScope
也应该有效:
mDialogView.add_button.setOnClickListener{
CoroutineScope(Dispatchers.IO).launch{
positiveButtonClick(mAlertDialog)
}
}
如果您对此仍有问题,请告诉我:)