问题描述
我有一个存储类型为Cup的对象的数组。 该按钮显示杯子中的numberOfPebbles值。 单击该按钮会使所有按钮增加1-我已经登录logcat并更新了数组,但是使用仿真器时我无法使用新的数组值来更新按钮。 这是我的代码:请记住它们在单独的类中
button1 = (Button)findViewById(R.id.button1);
button1.setText(move.cups.get(0).toString());
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
move.makeMove(move.cups.get(0));
}
});
public List<Cup> cups;
public List<Integer> cupValues;
public void newGame(){
cups = new ArrayList<>();
cupValues = new ArrayList<>();
//Reset all cups and values
for(int i=0; i<16; i++){
Cup cup = new Cup(7);
cups.add(i, cup);
Log.d("Move.java", "Printing array valuess:" + cups.get(i));
}
}
1楼
您需要在每次单击时更新按钮的文本
button1 = (Button) findViewById(R.id.button1);
button1.setText(move.cups.get(0).toString());
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
move.makeMove(move.cups.get(0));
button1.setText(move.cups.get(0).toString()); //or whatever you want to display in the button after each click
}
});