在复杂对象中查找对象

法鲁克·亚齐奇(Faruk Yazici)

我正在使用Justinmind的API开发插件。我需要的基本上是通过ID查找元素。但是,该API似乎没有像findById()这样的方法,因为我在这里搜索了整个文档我假设我必须创建一个方法。我在这里解释问题的细节和Justinmind的一般结构,尽管该问题并非特定于Justinmind

假设我具有以下页面布局:

在此处输入图片说明

我将按照以下层次结构在Justinmind中设计此页面:

Screen
----Image
----Group
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
----Text
----Text
----Text

在Justinmind API中,屏幕是ICanvas对象。我将屏幕存储在myScreen变量中。然后,getApiRoot()返回页面的最上面的组件。之后,每个组件都是一个子组件,可以通过getApiChildren()方法进行访问

List<IComponent> components = myScreen.getApiRoot().getApiChildren();

上面的代码返回“图像”,“组”和“文本”,即一阶hierchy组件。例如,我可以按以下方式访问第一个按钮的标签(概述)

IComponent group = components.get(1); //Returns the Group
IComponent button1 = group.getApiChildren().get(0); //Returns the first button group, which is the first child of group
IComponent label1 = button1.getApiChildren().get(1); //Returns the label, which is the second child of the first button group

如您所见,我总是通过getApiChildren()方法访问组件在大型动态页面中,这不是很可行。例如,因为我正在编写的插件会在按钮组中缺少标签时发出警报。为了检查标签的存在,我必须使用getApiChildren()方法遍历屏幕的所有组件

我可以通过一棵树来表示它:

在此处输入图片说明

但是,这种结构将始终发生变化。我应该建造一棵树还是一个哈希图?在非常复杂的对象中找到对象的最佳方法是什么?

索林克先生

我不知道Justinmind。但是,如果您在Java中有一个复杂对象图,则可以尝试使用此库http://commons.apache.org/proper/commons-jxpath/并使用XPATH语法指定查询。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章