迭代 AttributeSet 枚举

发布于 2024-11-09 19:23:44 字数 528 浏览 2 评论 0原文

我有以下代码:

    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;
    }

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

这里出了什么问题?

I have the following code:

    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;
    }

Now I would think this code would step through each of the attribute names. But it's only giving me every other attribute name (the ones with even-numbered indices).

What's going wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黯淡〆 2024-11-16 19:23:44

我不认为它有索引 - Set 没有索引。而且代码看起来不错。除非 getAttributeNames() 返回错误实现的枚举,否则它应该可以工作。

I don't think it has an index - a Set doesn't have index. And the code looks fine. Unless the getAttributeNames() returns a wrongly-implementation enumeration, it should work.

过度放纵 2024-11-16 19:23:44

目前我没有发现您的代码有任何问题,但请尝试使用 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;
}

I don't see anything wrong with your code at the moment, but try using 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;
}
草莓酥 2024-11-16 19:23:44

我怀疑这是java.util.Enumeration的问题(尽管这只是一个接口,实际的实现可能有bug)。你的实施对我来说看起来不错。

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

I doubt that this is a problem with java.util.Enumeration (although this is just an interface and the actual implementation may have a bug). Your implementation looks good to me.

Other idea: The initial AttributeSet may only contain every other attribute. Try to set a breakpoint and have a look at the internals of an actual attribute set.

月牙弯弯 2024-11-16 19:23:44

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

The internal list I was looking at in the debugger had both names and values alternating. So, my code is correct in a certain sense, but my intent was wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文