Datasource Table item error load

Hi,
I’m newby for Cuba Studio, I have a problem with the collection of Table item, the scenario is as follows:
main screen has one pickerField (pickerFieldA) and one table (tableB),
When the user select an element from pickerFieldA I would load data in the tableB depends from the value selected in the pickerFieldA.
To do this

@Inject
private Table<A> tableVariables;
 @Subscribe("pickerFieldA")
 public void onPickerFieldAValueChange(HasValue.ValueChangeEvent event) {
    tableB.setVisible(true);
    dataManager.load(A.class).query("select e from test_A e where e.id = " + 
                                     pickerFieldA.getValue());
        List<A> lists= dataManager.load(A.class).list();
        List<A> resultFinal = new ArrayList<>();
        for (A obj : lists){
            A variable = new Variable();
            variable.setName(obj.getName());
            variable.setDescription(obj.getDescription());
            resultFinal.add(variable);
        }
        tableVariables.setItems((TableItems<A>) resultFinal);
    }
}

The lists has three element and it’s correct.
I get the following error at this line

 tableVariables.setItems((TableItems<A>) resultFinal);

java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class com.haulmont.cuba.gui.components.data.TableItems (java.util.ArrayList is in module java.base of loader ‘bootstrap’; com.haulmont.cuba.gui.components.data.TableItems is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @a5b0b86)

Any suggest?
thanks in advance

Hello @qwerty_giggi!

You can use the query conditions to load data depending on the value in the PickerField component.
I gave an example of using:

  • The B entity has a Many-To-One relationship with A entity.
  • The screen contains the PickerField component for selecting an A entity and a table containing B entities that are associated with an A entity from the PickerField component.

XML descriptor

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/screen/window.xsd"
        xmlns:c="http://schemas.haulmont.com/cuba/screen/jpql_condition.xsd"
        caption="msg://testScreen.caption"
        messagesPack="com.company.sample.web.screens">
    <data readOnly="true">
        <collection id="bsDc"
                    class="com.company.sample.entity.B">
            <view extends="_local">
                <property name="a" view="_minimal"/>
            </view>
            <loader id="bsDl">
                <query>
                    <![CDATA[select e from sample_B e]]>
                    <condition>
                        <c:jpql>
                            <c:where>e.a.id = :aId</c:where>
                        </c:jpql>
                    </condition>
                </query>
            </loader>
        </collection>
    </data>
    <layout expand="bsTable" spacing="true">
        <pickerField id="pickerFieldA" caption="PickerField A" metaClass="sample_A"/>
        <groupTable id="bsTable"
                    width="100%"
                    dataContainer="bsDc"
                    caption="Table B">
            <columns>
                <column id="name"/>
                <column id="a"/>
            </columns>
            <rowsCount/>
        </groupTable>
    </layout>
</window>

Screen controller

@UiController("sample_TestScreen")
@UiDescriptor("test-screen.xml")
@LoadDataBeforeShow
public class TestScreen extends Screen {

    @Inject
    protected CollectionLoader<B> bsDl;

    @Subscribe("pickerFieldA")
    protected void onPickerFieldAValueChange(HasValue.ValueChangeEvent<A> event) {
        if (event.getValue() != null) {
            bsDl.setParameter("aId", event.getValue().getId());
        } else {
            bsDl.removeParameter("aId");
        }
        bsDl.load();
    }
}

Regards,
Gleb

Thanks Gleb it’s works.