Android how to wait for Method to finish before continu

I use the following code block in my program. I call the detectWinnerOfTheMatch method to do some checking.

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

The detectWinnerOfTheMatch method :

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();
        }
    }

And when it enters else state, I call the selectRandomPlayerFromPlayedMatch () method.

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);
    }

Before the selectRandomPlayerFromPlayedMatch () method ends, R.id.moveNextMatch: i ++; and startMatch (); method is working. How can I wait for the selectRandomPlayerFromPlayedMatch () method to finish. Code fails when it runs before it finishes.



from Recent Questions - Stack Overflow https://ift.tt/3sP3hDM
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Network Error and Timeout on Authorize.net JS

Spring Elasticsearch Operations