如何解决遍历AttributeSet枚举
| 我有以下代码: private static boolean hasTargetStyle(AttributeSet attributes) {
final Enumeration<?> attributeNames = attributes.getAttributeNames();
while (attributeNames.hasMoreElements()) {
final Object attributeName = attributeNames.nextElement();
if (attributeName.equals(MY_STYLE_NAME)) {
return true;
}
}
return false;
}
现在,我认为这段代码将逐步遍历每个属性名称。但这只是给我其他所有属性名称(带有偶数索引的属性名称)。
这是怎么了?
解决方法
我认为它没有索引-
Set
没有索引。代码看起来不错。除非getAttributeNames()
返回错误实现的枚举,否则它应该起作用。
,我目前看不到您的代码有什么问题,但请尝试使用Collections.list
private static boolean hasTargetStyle(AttributeSet attributes) {
final List<?> attributeNames = Collections.list(attributes.getAttributeNames());
for(Object name: attributeNames){
if(name.equals(MY_STYLE_NAME)){
return true;
}
}
return false;
}
,我怀疑这是java.util.Enumeration
的问题(尽管这只是一个接口,实际的实现可能会有错误)。您的实现对我来说很好。
其他想法:最初的“ 5”可能仅包含所有其他属性。尝试设置一个断点,并查看实际属性集的内部。
,我在调试器中查看的内部列表具有交替的名称和值。因此,我的代码在某种意义上是正确的,但我的意图是错误的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。