1 回答

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
問題似乎來自CountDownLatch,它阻塞了線程,你應(yīng)該在你的自定義對話框類中創(chuàng)建一個(gè)接口,在那里你有一個(gè)(或者你想調(diào)用它的任何內(nèi)容)你在其他地方實(shí)現(xiàn)。onResult()
您必須將一個(gè)偵聽器傳遞給您的實(shí)現(xiàn),并在您的OK按鈕中調(diào)用showDialog()onResult()onClicklistener.onResult()
接口(在自定義輸入對話框中.java):
interface CustomInputDialogListener{
void onResult(ArrayList<String> result);
}
要傳遞給顯示的新參數(shù):
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");
您可以刪除閂鎖創(chuàng)建和嘗試/捕獲塊,并在末尾使用等待和返回語句
//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;
}
}*/
在您的主要活動(dòng)中.java您有2個(gè)選擇:
實(shí)現(xiàn)自定義輸入對話框中的接收器并將其傳遞給showDialog
您的主要活動(dòng)的標(biāo)題應(yīng)如下所示:
public class MainActivity extends AppCompatActivity implements CustomInputDialog.CustomInputDialogListener {
...
}
并且您必須在搜索結(jié)果() 上實(shí)現(xiàn):
@Override
public void onResult(ArrayList<String> result) {
this.result = result;
doThings();
}
當(dāng)你調(diào)用顯示方言()時(shí),你傳遞這個(gè):
CustomInputDialog.showDialog(Test14dialog.this, "Title", fields, this);
您直接實(shí)現(xiàn) onResult :
CustomInputDialog.showDialog(Test14dialog.this, "Title", fields, new CustomInputDialog.CustomInputDialogListener() {
@Override
public void onResult(ArrayList<String> result) {
this.result = result;
doThings();
}
});
顯示對話框時(shí)不應(yīng)阻止線程
添加回答
舉報(bào)