Home > Back-end >  Android - User input at specific point in method
Android - User input at specific point in method

Time:04-12

I am programming a darts counter app and at the moment I am trying to get an user input (which field on the dart board they hit) by pressing on a specific button. Each button press will return an int which will be used to update list values that are used by my adapter to then update the views.

The method that should happen in looks like this:

private void startRound(MatchActivityPlayerAdapter adapter) {

        for (int playerIndex = 0; playerIndex < getMatchParticipants().size(); playerIndex  ) {
            for (int currentDart = 1; currentDart <= maximumDartsToThrow; currentDart  ) {

                // Here I want the activity to "wait" until the user presses a button

                if (pointsButtonClicked) {
                    setDartValue(playerIndex, currentDart);
                    setDartsCombinedValues(playerIndex, currentDart);
                    setRemainingPointsValues(playerIndex, currentDart);

                    adapter.notifyDataSetChanged();
                }
            }
        }
        setCurrentRound(getCurrentRound()   1);
    }

Since I cant really stop the activity at the point mentioned above until user has made an input I'll propably have to include a while loop. But in order to do this I think I'll have to create a second thread and handle things differently. That actually overwhelmed me, even though I've been reading through this.

Can anyone please explain to me how I have to design my code in order to achieve what i want?

Edit: Actually pointsButtonClicked is a boolean. I have over 20 buttons in the global OnClick method and whenever one of them is clicked pointsButtonClicked will be set to true.

Button button1 = findViewById(R.id.btn1);
button1.setOnClickListener(v -> {
    outputInt = 1;
    pointsButtonClicked = true;
});

Button button2 = findViewById(R.id.btn2);
button2.setOnClickListener(v -> {
    outputInt = 2;
    pointsButtonClicked = true;
});

// [...]

CodePudding user response:

I think that there should be a better way to this without the while-wait loop.

First of all I suppose that you used an android.widget.Button to implement that pointsButtonClicked

Here's what I would do:

//Global in your activity
int playerIndex = 0;
int currentDart = 1;

Then

private void startRound(MatchActivityPlayerAdapter adapter) 
{
    pointsButtonClicked.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            setDartValue(playerIndex, currentDart);
            setDartsCombinedValues(playerIndex, currentDart);
            setRemainingPointsValues(playerIndex, currentDart);
            adapter.notifyDataSetChanged();

            nextThrow();
        }
    });
}

And finally

//Also in your activity
private void nextThrow()
{
    if (currentDart == maximumDartsToThrow)
    {
        currentDart = 1;
        if (playerIndex == getMatchParticipants().size()-1)
        {
            playerIndex = 0;
            setCurrentRound(getCurrentRound()   1);
        }
        else
        {
              playerIndex;
        }
    }
    else
    {
          currentDart;
    }
}

It can be better

You can use private variables and access them with getter and setter

Explained

Using this approach you do not create a thread that waits for every single button pressed. You simply create a button that listen for every click event that it receives and then you apply the logic to cycle the players and the darts for every players.

I hope this could be helpful.

[EDIT]

In this case I would apply the same OnClickListener to every button you use.

First of all I would create an inner class that implements OnClickListener and allows you to manage outputInt variable.

//Also in Activity (it is an inner class)
public class MyOnClickListener implements View.OnClickListener
{
    private int myOutputInt;
    public MyOnClickListener (int outputInt)
    {
        this.myOutputInt = outputInt;
    }
    @Override
    public void onClick(View v)
    {
        pointsButtonClicked = true;
        outputInt = myOutputInt;
        setDartValue(playerIndex, currentDart);
        setDartsCombinedValues(playerIndex, currentDart);
        setRemainingPointsValues(playerIndex, currentDart);
        nextThrow();
    }
}

And then you create 20 MyOnClickListener passing only the int you want to assign. For example:

button1.setOnClickListener(new MyOnClickListener(1));
button2.setOnClickListener(new MyOnClickListener(2));
button3.setOnClickListener(new MyOnClickListener(3));
etc etc...
  • Related