Is it possible to get children component list of an arrangement?

Hi there.
Is it possible to get children component list of an arrangement by Java/extension?
So that i can use the any component block to manipulate item of the list.
Thanks
Kevin

At the moment containers don't actually keep track of their child components, but it should be possible in the next release as it adds a getChildren() method to ComponentContainer. You can sort of hack it if you use Java reflection to extract the list of all components, and then for every component that extends AndroidViewComponent use reflection to gets it container field and filter for the target container.

6 Likes

here is the code -

HVArrangement container;
final View v = container.getView();
FrameLayout frameLayout = (FrameLayout) v;
List<View> views = new ArrayList<View>();
final int childCount = frameLayout.getChildCount();
for (int i = 0; i < childCount; i++) {
      View vi = ll.getChildAt(i);
      views.add(vi);
}
1 Like

Thanks and waiting next release

1 Like

Have you test it? This list will get a java native view, not app inventor component.

well i could have only found this i will do more research

I have tried this:

	@SimpleFunction

public List ChildreList(HVArrangement component) {
List childrenList = new ArrayList();
ViewGroup subGroup = (ViewGroup) ((ViewGroup) component.getView()).getChildAt(0);
for (int i = 0; i < subGroup.getChildCount(); i++) {
childrenList.add(subGroup.getChildAt(i).getClass().getName());
}
return childrenList;
}

and I got this:


but I want to get like this:
[com.google.appinventor.components.runtime.Button,com.google.appinventor.components.runtime.Label]

1 Like

View and AndroidViewComponent are different.

2 Likes

idk but will assigning like this will work ? -

android.widget.Button androidButton;
com.google.appinventor.components.runtime.Button appinventorButton = (com.google.appinventor.components.runtime.Button) androidButton;
1 Like

Don't think widget Button can be casted to ai component Button.

2 Likes

No. The two classes have no common ancestors (except java.lang.Object). Casting of that nature is safe if you're casting to a superclass/superinterface of the instance's type, and it's unsafe if you cast down to a known subclass of the type, but in this case it is illegal because there's no relationship between the two types. You'll likely get an error from the Java compiler to this effect.

4 Likes

maybe this topic helps - Converting a view to AndroidViewComponent

1 Like

I basically say the same thing in that thread as I am saying in this one. You cannot cast from one type to the other because they have no common ancestry in the class hierarchy.

1 Like