android - 单击时从列表中获取单个整数

标签 android arraylist

这是我的数组列表/列表..当我点击按钮时我想要单个值并且列表中的值应该加在一起..

[1, 1, 3, 3, 3, 3, 1, 1, 3, 1, 4, 3, -1, 4, 1, 4, 1, 2, 4, 1, 1, 1, 4, 1, 1]

我尝试过的事情

for (int i = 0; i < Score1.size(); i++) {

              score = Score1.get(i);
              score2 += score;
}

输出:

total﹕ 51

但这件事应该只发生在按钮点击时.. 我尝试了一些东西..但是当我点击按钮时..total comes all together..but I don't want that.

要求的输出-

total=0
total=1
total=2
total=5
etc...

最佳答案

全局声明

int  i = 0;
int score = 0;

在你的方法中

    button.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if(i<Score1.size()){
                                    score = Score1.get(i);
                                    i++;
                                    System.out.println(score); // or any task you want to do
                                    }
                                }
                            });

score 中,您的添加将在每次点击后存储。

关于android - 单击时从列表中获取单个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33518543/

相关文章:

android - Android 4.x 中的 TextView 字体

android - 如何在 dp 值中设置 Layoutparams 高度/宽度?

java - 如何遍历 Android ArrayList 并获取索引

android - 无法使用 Gson 从另一个 Activity 的共享首选项中删除数据

android - 有没有人使用 OpenCV 实现了一个用于统一的 android 插件?

android按钮setonclicklistener

android - textview android 中的 ResourceNotFoundException

javascript - Chart.js无法显示数据

java - 在 Java 中从 ArrayList 创建并返回值

java - 它写入列表的第一条记录或最后一条记录,任何正确的建议