Home > OS >  Android Studio: Use a Button in a for loop
Android Studio: Use a Button in a for loop

Time:12-15

I have a class, called "Manager" that passes two values. Once the Integer "mode" and the string "description". This data is then passed to the "description" method. The method contains the array list "test":

public class Manager  {

    public int mode;
    public String description;


    public Manager(int mode, String description) {
        this.mode = mode;
        this.description = description;

    }
}

This data is then passed to the "description" method. The method contains an arraylist "test":

public ArrayList<Manager> description() {
    ArrayList<Manager> test = new ArrayList<>();
    test.add(new Manager(1,"Test 1"));
    test.add(new Manager(2,"Test 2"));
    test.add(new Manager(3, "Test 3"));
    shuffle(test);
    return test;
}

In the method "print", it is iterated over the ArrayList. Depending on which mode is passed, something different is output in the text view.

public void print(List<Manager>test) {
    for(Manager i : test) {
        if(i.mode == 1) {
            descriptions.setText("Mode 1 "   i.description);
        } else if(i.mode == 2) {
            descriptions.setText("Mode 2 "   i.description);
        } else if(i.mode == 3D) {
            descriptions.setText(("Mode 3 "   i.description));
        }
    }
}

Now I want to use a button to output a random element from the array list after each click until all elements have been output or until the loop is finished. So far I have a buttonClicked method that calls the "print" method after each click. But here, the method is restarted after each click.

How can I use the button in the loop so that I get each item only once after each click until the loop is over?

CodePudding user response:

Here still the "buttonclicked" method

public void buttonClicked(View view) {
    print(description());
}

CodePudding user response:

Now I want to use a button to output a random element from the array list after each click until all elements have been output or until the loop is finished. So far I have a buttonClicked method that calls the "print" method after each click. But here, the method is restarted after each click.

This is because you are creating a brand new list with (description()) and printing all its contents every single button click.

How can I use the button in the loop so that I get each item only once after each click until the loop is over?

I don't know what this means.

  • Related