java - 对象值更改后更新 Android 中的按钮?

标签 java android arrays arraylist

我有一个存储 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));
    }
}

最佳答案

每次点击时您都需要更新按钮的文本

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
    }
});

关于java - 对象值更改后更新 Android 中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33215368/

相关文章:

java - PatternSyntaxException : Unclosed Group near Index 1. 为什么我的检查不起作用?

java - 如何生成多个工作线程并将它们绑定(bind)到 Heroku 上的非 Web Java 应用程序

android - 为什么我不能从 json Retrofit 中获取特殊字段?

arrays - 数组元素名称数量与数组对象的增量值

使用类型为 "struct student"的数组参数进行 C 编程与类型为 "struct student *"的参数不兼容错误

c++ - 在循环外存储 C++ 数组

java - Java异常安全可以变得不那么难看吗?

java - 仅在一个浏览器中运行 java web 应用程序

android - 应用程序更新后小部件消失了

android - 如何在 Android XML 中自动镜像形状