简体   繁体   中英

Wait for 2 click events until proceed doesn't work

Following situation:

I code a Domino game for Android. At the beginning of the game, if not a bot is the starting player, a user should pick two Dominos that get onto the board.

Overview of my non-working approach:

Definition of an empty ArrayList, call of a function that attaches Click Listeners on every Domino belong to the user followed by a while loop that does nothing, should just be the mechanism to wait until the user has picked two dominos. These dominos should be stored in the ArrayList(adding of the dominos to the ArrayList in FirstDominosPickerListener)

The code

In the activity:

 ArrayList<Domino> starterDominos = new ArrayList<Domino>();  

    startingPlayer.chooseStartDominos(starterDominos);

the function:

   public void chooseStartDominos(ArrayList<Domino> starterDominos){   


        ///Every Domino gets a ClickListener
        for (Domino domino : playerSet){
            domino.setOnClickListener(new FirstDominosPickerListener( starterDominos));
        }

        //The idea is to wait until the user has picked two Dominos. With that loop, no UI at all shows up
        while (starterDominos.size()<2){

            Log.v(LOG_TAG," WAIT!!!!");
        }        


    }

The problem is the while Loop. With the loop, no UI shows up, i get an empty white screen, altough the code runs. In logcat, i get infinite "Wait" messages. No idea why.

A second approach I've tried was to call a Timer task in the activity after that checks if two dominos were picked (through the size of the list) starterDominos = startingPlayer.chooseStartDominos(starterDominos);

I realized that couldn't work because that runs in another thread and because of that, it wasn't possible to access any part of the UI. But the mechanism to pick the dominos worked. UI showed up and run() ended after two dominos were picked through cancel() .

So why does the while loop leads to that behavior? Is the whole approach wrong and if so what can I do that the app waits until the dominos were picked and then proceed

Your while loop is blocking the main UI thread so nothing can be drawn or updated on the screen. You need to set up a listener for when a user selects a domino, then proceed after a user has selected two dominos.

You can use a class variable in the activity class 'userDominoSelectCount' which can be a short integer field initialized to 0.

You can add the onClickListener for domino in the activity onCreate method itself:

domino.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
      ++userDominoSelectCount;
      if(userDominoSelectCount == 2){

        //initiate AI logic
        userDominoSelectCount = 0;
      }
  }
});

The way you are doing will block the main thread as mentioned in other answer to the question. OnClicklisteners are meant to be run in event loop and ideally should not block UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM