2011-05-25 66 views
1

我有以下代码:遍历一个枚举的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; 
    } 

现在我认为这个代码将逐步完成每个属性名称的。但它只给我所有其他属性名称(具有偶数索引的名称)。

这里怎么回事?

+0

你的代码看起来不错;这个问题可能与输入'AttributeSet'有关。 – 2011-05-25 14:22:07

+0

我们可以看到'AttributeSet'吗? – mre 2011-05-25 14:26:58

回答

1

我不认为它有一个索引 - 一个Set没有索引。代码看起来很好。除非getAttributeNames()返回一个错误的实现枚举,它应该工作。

1

我没有看到任何你的代码错误的时刻,但尝试使用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; 
} 
1

我怀疑这是java.util.Enumeration问题(虽然这只是一个接口,实际实现可能有一个错误)。你的实现对我来说很好。

其他想法:初始AttributeSet可能只包含其他所有属性。尝试设置断点并查看实际属性集的内部结构。

0

我在调试器中看到的内部列表具有交替名称和值。所以,我的代码在某种意义上是正确的,但我的意图是错误的。