【发布时间】:2015-08-08 00:22:50
【问题描述】:
好的,这会变得有点复杂,但请耐心等待。我有以下情况无法上班:
- 提示用户保存(确定或取消)
- 在“ok”的 onclick 中,执行异步任务以获取一些数据。同步执行此操作
- 然后创建一个自定义对话框来显示该数据以提示用户提供更多信息 [这部分不起作用)
- 在我保存数据的地方执行另一个异步任务。
那么,在 AlertDialog 中创建自定义对话框是否存在固有问题?
发生的行为是所有操作都运行,最后自定义对话框短暂出现然后消失。它应该出现在最后一个 Async 任务之前,并且在 Async 任务收集到所需数据之前不让其执行。
我已将问题代码简化为:
AlertDialog.Builder submitDialog = new AlertDialog.Builder(this);
submitDialog.setMessage("Are you sure you want to end this Preview of the form? It will no longer be availabe for editing");
}
else {
submitDialog.setTitle("Submit?");
submitDialog.setMessage("Are you sure?");
}
submitDialog.setNegativeButton("Cancel", null);
submitDialog.setPositiveButton("OK", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
// =============================
// 1st Async
// =============================
// Make synchronous since need data. Must be a task cause hitting server
Test test = null;
try {
test = new workflowTask().execute().get();
}
catch (Exception ex) {
// nothing
}
// =============================
// Customer Dialog Get more info
// =============================
// Prompt for data here...
String[] listContent = {"test1@test.com", "test2@test.com"};
// custom dialog
Dialog dialog = new Dialog(MyActivity.this);
dialog.setContentView(R.layout.dialog_email_picker);
dialog.setTitle("Select email and enter comments");
ListView emails = (ListView) dialog.findViewById(R.id.emaillist);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (MyActivity.this, android.R.layout.simple_list_item_1, listContent);
emails.setAdapter(adapter);
emails.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), parent.getItemAtPosition(position).toString() + " clicked", Toast.LENGTH_LONG).show();
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "ok clicked", Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
// =============================
// 2nd Async
// =============================
new submitFormResultTask().execute();
}
}
});
submitDialog.show();
【问题讨论】:
-
为什么不在获取数据后关闭第一个异步任务的 postExecute 上的警报对话框?然后在您询问更多信息时显示带有检索数据的自定义对话框。这样你就完全避免了这个问题。
-
有点呼应@DigitalNinja,我认为你应该改变你的用户界面。同时或快速连续的多个对话不是良好的用户体验。使用 Activity 或 single 自定义对话框来处理整个流程(并且可能会在整个持续时间内停留在屏幕上)。您可以在用户提交时禁用输入字段,并在第一个 AsyncTask 工作时显示微调器。
-
@DigitalNinja。我试过了 - 在 postExecute 中关闭原始警报对话框但没有成功。不知道我在那里做错了什么。
-
@StephenMcCormick 我对对话框和任务做了类似的事情。我不知道如何在没有看到它的情况下提供任何帮助。你还在为这个烦恼吗?
-
@DigitalNinja 我想我已经解决了。我相信问题在于执行以下操作:AlertDialog -> AsyncTask -> Dialog -> AsyncTask。我将最后一个 AsyncTask 移到了 Dialog 的 onClick 中,一切都很开心。虽然也许您或其他人可以解释为什么会这样。
标签: android android-asynctask dialog android-alertdialog