2011-05-25 69 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

我在調試器中看到的內部列表具有交替名稱和值。所以,我的代碼在某種意義上是正確的,但我的意圖是錯誤的。