开发者

Why is it discouraged to use Resources.getIdentifier()

In the Android SDK, there is a comment that says it's more e开发者_JAVA百科fficient to retrieve resources by identifier instead of by name.

Is this the only reason it's discouraged to use getIdentifier()?

I need to programmatically access one of several hundred resources and so far my design makes it easier to access raw resources by name instead of identifier.


Because comparing integers is much faster than comparing strings and I would guess that the resources are hashed by their ID, so if you get a resource by ID they can grab only a small subset of resources and perform a fast search. If you search by name they'd have to iterate through all the resources and do a slow string compare.


Yes, getIdentifier() is slower/less efficient than just retrieving the resource by ID, I think it uses reflection to get the identifier from the generated R class. But I'm not aware of any other reason not to use it... Look into array resources, maybe you can use those instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜