[英]How to wait for Android Dialog to finish?
我正在做一個逃脫游戲,我有一個 useObjectButton。 單擊時,它會顯示一個帶有 ListView 的對話框,我希望它在玩家單擊對話框的某個項目后更新信息。 但是,它會立即更新,我不知道如何修復它。
我的對話框如下所示:
public Inventory.ITEM selectItem() {
if (game.inventory.isEmpty()) {
Toast.makeText(this, "Inventory is empty", Toast.LENGTH_SHORT).show();
return null;
} else {
AlertDialog inventoryDialog;
AlertDialog.Builder inventoryDialogBuilder = new AlertDialog.Builder(ActivityGame.this);
View inventoryPopup = getLayoutInflater().inflate(R.layout.inventory, null, false);
inventoryDialogBuilder.setView(inventoryPopup);
inventoryDialog = inventoryDialogBuilder.create();
inventoryDialog.setCancelable(true);
ListView lvInventory = inventoryPopup.findViewById(R.id.inventory_lv);
if (!game.inventory.isEmpty()) {
ArrayAdapter adapter = new ArrayAdapter(ActivityGame.this, android.R.layout.simple_list_item_1, game.inventory.getInventory());
lvInventory.setAdapter(adapter);
lvInventory.setOnItemClickListener((parent, view, position, id) -> {
selectedItem = game.inventory.getItem(position);
inventoryDialog.dismiss();
});
inventoryDialog.show();
}
}
}
我在各種函數中調用它,例如:
btnUseObject.setOnClickListener(v -> {
Inventory.ITEM item=selectItem();
info.setText(game.nightStand.useObject(item));
if(game.nightStand.isUnlocked()) {
btnDrawer1.setEnabled(true);
btnDrawer2.setEnabled(true);
}
});
首先,您必須了解這些指令的異步性質:
Inventory.ITEM item=selectItem();
和
lvInventory.setOnItemClickListener((parent, view, position, id) -> {
selectedItem = game.inventory.getItem(position);
inventoryDialog.dismiss();
});
您不能讓此方法返回 selectedItem 的值,因為它在執行時是未知的。 將單擊偵聽器綁定到按鈕的時間與用戶實際單擊按鈕的操作不同。 你應該做的是給一個 function 作為異步 function 的參數,當用戶單擊按鈕時,它會被調用。 像這樣的東西:
public void selectItem(Consumer callbackFn) {
if (game.inventory.isEmpty()) {
Toast.makeText(this, "Inventory is empty", Toast.LENGTH_SHORT).show();
return;
}
AlertDialog inventoryDialog;
AlertDialog.Builder inventoryDialogBuilder = new AlertDialog.Builder(ActivityGame.this);
View inventoryPopup = getLayoutInflater().inflate(R.layout.inventory, null, false);
inventoryDialogBuilder.setView(inventoryPopup);
inventoryDialog = inventoryDialogBuilder.create();
inventoryDialog.setCancelable(true);
ListView lvInventory = inventoryPopup.findViewById(R.id.inventory_lv);
ArrayAdapter adapter = new ArrayAdapter(ActivityGame.this, android.R.layout.simple_list_item_1, game.inventory.getInventory());
lvInventory.setAdapter(adapter);
lvInventory.setOnItemClickListener((parent, view, position, id) -> {
// The asynchronous function that gets called whenever an user clicks a button.
// You cannot return a value from it because it is not immediately known at execution time.
callbackFn.accept(game.inventory.getItem(position));
inventoryDialog.dismiss();
});
inventoryDialog.show();
}
接着
btnUseObject.setOnClickListener(v -> {
Consumer callbackFn = (Inventory.ITEM item) -> {
info.setText(game.nightStand.useObject(item));
if (game.nightStand.isUnlocked()) {
btnDrawer1.setEnabled(true);
btnDrawer2.setEnabled(true);
}
};
selectItem(callbackFn); // When an user clicks the button, please execute my callback function
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.