我在这里面对的一个愚蠢问题的种类...基本上我在For循环中,并在此循环中,我总是调用一个函数来制作一个按钮。但在这个函数中,我想通过循环迭代器,因为它正在改变区分按钮。但是,然后它告诉我我需要使循环迭代器“最终”,这意味着它不会改变!Java - 获取循环迭代器到循环内的函数?
那么我的问题会更有意义一些骨架代码:
for(int i = 0; i < appList.size(); i++) {
//some other stuff including creating the below button 'btn'
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//set some attribute based on i
//but when I do this it only sets based on the greatest value of i (!)
}
});
}
咩有点傻问题,我知道...我是新来的Java,但!
干杯。
的评论说,你将设置基于i *使用的最大价值我*似乎不准确的属性。由于答案中缺少关键字'final',因此您无法编译包含代码段的代码。不过,您可能已经将整数包装在最终的单个元素数组中,并且会得到您描述的行为。 – Atreys 2011-06-14 15:37:04