java - 获取 2D ImageButton ArrayList 的位置
问题描述
我有一个名为ArrayList<ArrayList<ImageButton>> boardImageButtons
.
在该方法onClick(View view)
中,我将单击的图像按钮命名为 ((ImageButton) view) btn
。
我想获得该对象在 2D ArrayList 中的位置( i
& )。j
我尽量避免运行 throw 所有 2D ArrayList,比较对象并获取 equals 对象的i
& j
。
你有更好的建议吗?
有什么方法可以传递额外的变量onClick(View view)
吗?
解决方案
在您的应用程序开始时创建一个HashMap
。
class Pair {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
Map<ImageButton, Pair> map = new HashMap<?>();
通过使用一些循环或您的逻辑放置所有值来初始化地图
map.put(btn1, new Pair(1, 2));
..
..
在检索位置时,使用
Pair myPair = map.get(view);
推荐阅读
- php - 如果不存在数据,则显示大小为 0 的数组,而不是大小为 1 的数组 (array(1) { [0]=> string(0) "" }) || PHP
- django - 将已批准的电子邮件点编辑到 localhost
- php - PHP:如果发生异常则撤消
- swift - 通用可解码,无需解码
- python - 如何在 keras 中重塑一维卷积层的数据?
- javascript - 找不到带有角度路径的控件
- hibernate - Hibernate @ManyToOne 总是用referencedColumnName 加载实体
- r - 来自 R 代码块的动态 RMarkdown 标题
- charts - 在 Chart.js 中的图表内显示标签的总计(或简单地显示一个字符串)
- c++ - std::variant 的 std::vector 的内存布局如何?