我想执行一个函数本身n次,该函数负责显示一个Dialog,但是当前同时生成该数量的Dialog,我希望显示该Dialog,并在关闭后打开下一个对话框再一次,不是所有人都一口气。
对此有何建议?
这是我此功能的主要代码
private fun onReportNotification(){
showMessageDialog(ConfirmationDialog.DIALOG_REPORT_CREATED, getString(R.string.service_line_popup_push_title), getString(R.string.service_line_popup_push_body), getString(R.string.login_password_btn2),"")
}
override fun onResume() {
super.onResume()
checkForPendingTicketsToEvaluate()
}
private fun checkForPendingTicketsToEvaluate(){
ServiceLineRepository.getAllReportsNotAualfied(applicationContext, serverClient){
boolean: Boolean, unQualifiedReports: ArrayList<Entity.ServiceRequestedElement> ->
if(boolean){
unQualifiedReports.forEach {
async {
currentId = it.id
currentService = it.requestedService
onReportNotification()
}
}
} else {
}
}
}
这是showMessageDialog()的代码段
open fun showMessageDialog(
dialogType: String,
title: String,
body: String,
acceptBtn: String,
cancelBtn: String
) {
Log.d(TAG, "showErrorDialog started, body=$body")
val ft = supportFragmentManager.beginTransaction()
val prev = supportFragmentManager.findFragmentByTag(ConfirmDialog)
if (prev != null) {
ft.remove(prev)
}
ft.addToBackStack(null)
val arg = Bundle()
arg.putString(ConfirmationDialog.DIALOG_TYPE_KEY, dialogType)
arg.putString(ConfirmationDialog.DIALOG_TEXT_BODY_KEY, body)
arg.putString(ConfirmationDialog.DIALOG_TEXT_TITLE_KEY, title)
arg.putString(ConfirmationDialog.DIALOG_TEXT_ACEEPT_BTN, acceptBtn)
arg.putString(ConfirmationDialog.DIALOG_TEXT_CANCEL_BTN, cancelBtn)
val newFragment = ConfirmationDialog.newInstance(arg)
try {
ft.let { newFragment.show(it, ConfirmDialog) }
Log.d(TAG, "showMessageDialog, showing dialog")
} catch (e: Exception) {
e.printStackTrace()
e.message?.let {
Log.e(TAG, it)
}
}
}
谢谢
答案 0 :(得分:0)
这是我能想到的,可能不是最佳实践。但是我认为这可以解决您的问题。
首先,将数组大小传递给onReportNotification()
函数,并像这样将其从循环中移出
if(boolean){
unQualifiedReports.forEach {
async {
currentId = it.id
currentService = it.requestedService
}
}
onReportNotification(unQualifiedReports.size)
} else {
}
然后,在onReportNotification()
函数中添加数组大小参数也将其传递给showMessageDialog()
方法
private fun onReportNotification(totalDialogToBeShown: Int){
showMessageDialog(ConfirmationDialog.DIALOG_REPORT_CREATED, getString(R.string.service_line_popup_push_title), getString(R.string.service_line_popup_push_body), getString(R.string.login_password_btn2),"", totalDialogToBeShown)
}
最后,在您的showMessageDialog()
函数中,将其更改为此
open fun showMessageDialog(
dialogType: String,
title: String,
body: String,
acceptBtn: String,
cancelBtn: String,
totalDialogToBeShown: Int
) {
Log.d(TAG, "showErrorDialog started, body=$body")
val ft = supportFragmentManager.beginTransaction()
val prev = supportFragmentManager.findFragmentByTag(ConfirmDialog)
if (prev != null) {
ft.remove(prev)
}
ft.addToBackStack(null)
val arg = Bundle()
arg.putString(ConfirmationDialog.DIALOG_TYPE_KEY, dialogType)
arg.putString(ConfirmationDialog.DIALOG_TEXT_BODY_KEY, body)
arg.putString(ConfirmationDialog.DIALOG_TEXT_TITLE_KEY, title)
arg.putString(ConfirmationDialog.DIALOG_TEXT_ACEEPT_BTN, acceptBtn)
arg.putString(ConfirmationDialog.DIALOG_TEXT_CANCEL_BTN, cancelBtn)
val newFragment = ConfirmationDialog.newInstance(arg)
var numberOfShownDialog = 1
newFragment.onDismiss(object : DialogInterface {
override fun dismiss() {
if (numberOfShownDialog < totalDialogToBeShown) {
numberOfShownDialog++
ft.let { newFragment.show(it, ConfirmDialog) }
}
}
override fun cancel() {}
})
try {
ft.let { newFragment.show(it, ConfirmDialog) }
Log.d(TAG, "showMessageDialog, showing dialog")
} catch (e: Exception) {
e.printStackTrace()
e.message?.let {
Log.e(TAG, it)
}
}
}
希望这可以解决您的问题:)