【发布时间】:2022-01-18 00:10:21
【问题描述】:
我有一个名为“Manager”的类,它传递两个值。一次是整数“模式”和字符串“描述”。然后将此数据传递给“描述”方法。该方法包含数组列表“test”:
public class Manager {
public int mode;
public String description;
public Manager(int mode, String description) {
this.mode = mode;
this.description = description;
}
}
然后将此数据传递给“描述”方法。该方法包含一个数组列表“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;
}
在“print”方法中,它在 ArrayList 上进行迭代。根据传递的模式,文本视图中会输出不同的内容。
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));
}
}
}
现在我想在每次点击后使用一个按钮从数组列表中输出一个随机元素,直到所有元素都被输出或循环完成。 到目前为止,我有一个 buttonClicked 方法,它在每次单击后调用“print”方法。但是在这里,每次点击后都会重新启动该方法。
如何使用循环中的按钮,以便在每次单击后仅获取每个项目一次,直到循环结束?
【问题讨论】:
标签: java android-studio button arraylist foreach