android如何在继续之前等待方法完成

bq3bfh9z  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(171)

我在程序中使用以下代码块。我调用detectWinnerOfMatch方法进行一些检查。

case R.id.moveNextMatch: {     
 detectWinnerOfTheMatch(matches.get(i).getHomeTeamGoal(),matches.get(i)
.getAwayTeamGoal());
            i++;
            startMatch();
            break;
        }

匹配方法:

private void detectWinnerOfTheMatch(int homeGoal, int awayGoal) {

        if(homeGoal > awayGoal){
            matches.get(i+1).setNameOfHomeTeam(matches.get(i).getNameOfHomeTeam());
            Match m = new 
            Match(getString(R.string.next_opponent),matches.get(i).getNameOfAwayTeam());
            matches.add(m);
        }else if(awayGoal> homeGoal){
            matches.get(i+1).setNameOfHomeTeam(matches.get(i).getNameOfAwayTeam());
            Match m = new 
            Match(getString(R.string.next_opponent),matches.get(i).getNameOfHomeTeam());
            matches.add(m);
        }else {
            selectRandomPlayerFromPlayedMatch();
        }
    }

当它进入else状态时,我调用selectrandomplayerfromplayedmatch()方法。

private void selectRandomPlayerFromPlayedMatch() {
        final SweetAlertDialog alertDialog = new SweetAlertDialog(this, 
        SweetAlertDialog.PROGRESS_TYPE);
        alertDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        alertDialog.setTitleText(getString(R.string.theWinnerStayDrawMatch));
        alertDialog.setCancelable(false);
        alertDialog.show();

        new CountDownTimer(5000, 200) {
            public void onTick(long l) {
                Random rand = new Random();
                 number = rand.nextInt(4);
                if(number %2 == 0){
                    alertDialog.setContentText(getString(R.string.selectedPlayer)
                            +matches.get(i).getNameOfHomeTeam());
                }else {
                    alertDialog.setContentText(getString(R.string.selectedPlayer)
                            +matches.get(i).getNameOfAwayTeam());
                }
            }

            public void onFinish() {
                alertDialog.setTitleText("a")
                        .setConfirmText("b")
                        .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);

            }
        }.start();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                alertDialog.dismissWithAnimation();
                matches.get(i+1).setNameOfHomeTeam(matches.get(i).getNameOfHomeTeam());
                Match m = new 
                Match(getString(R.string.next_opponent),matches.get(i).getNameOfAwayTeam());
                matches.add(m);

            }
        },6000);
    }

在selectrandomplayerfromplayedmatch()方法结束之前,r.id.movenextmatch:i++;和startmatch();方法有效。如何等待selectrandomplayerfromplayedmatch()方法完成。代码在未完成之前运行时失败。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题