1 回答

TA贡献1858条经验 获得超8个赞
问题似乎来自CountDownLatch,它阻塞了线程,你应该在你的自定义对话框类中创建一个接口,在那里你有一个(或者你想调用它的任何内容)你在其他地方实现。onResult()
您必须将一个侦听器传递给您的实现,并在您的OK按钮中调用showDialog()onResult()onClicklistener.onResult()
接口(在自定义输入对话框中.java):
interface CustomInputDialogListener{
void onResult(ArrayList<String> result);
}
要传递给显示的新参数:
static void showDialog(final Context context, String title, final ArrayList<Field> fields, final CustomInputDialogListener listener) {
...
}
单击“确定”按钮的末尾:”
dialog.dismiss();
listener.onResult(result);
//latch.countDown(); //you don't need that anymore
Log.d(TAG, "showDialog: latch count down 1");
您可以删除闩锁创建和尝试/捕获块,并在末尾使用等待和返回语句
//Log.d(TAG, "showDialog: latch created");
//final CountDownLatch latch = new CountDownLatch(1);
/*try {
Log.d(TAG, "showDialog: latch await");
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (result.isEmpty()) {
return null;
} else {
return result;
}
}*/
在您的主要活动中.java您有2个选择:
实现自定义输入对话框中的接收器并将其传递给showDialog
您的主要活动的标题应如下所示:
public class MainActivity extends AppCompatActivity implements CustomInputDialog.CustomInputDialogListener {
...
}
并且您必须在搜索结果() 上实现:
@Override
public void onResult(ArrayList<String> result) {
this.result = result;
doThings();
}
当你调用显示方言()时,你传递这个:
CustomInputDialog.showDialog(Test14dialog.this, "Title", fields, this);
您直接实现 onResult :
CustomInputDialog.showDialog(Test14dialog.this, "Title", fields, new CustomInputDialog.CustomInputDialogListener() {
@Override
public void onResult(ArrayList<String> result) {
this.result = result;
doThings();
}
});
显示对话框时不应阻止线程
添加回答
举报