2
votes

I'm still a newbie to Adobe Air/Flex, and still fairly new with SQL.

I've downloaded this (http://coenraets.org/blog/2008/11/using-the-sqlite-database-access-api-in-air…-part-1/) code and have been looking over it and I'm trying to implement a different idea.

I'm using a canvas for this form.

MaterialForm.mxml is called by a panel CadastroMaterial.mxml which also show a datagrid with the information saved from this form.

the sql statements are made by MaterialEvent.as using the lib flexlib.

I'm having a problem getting data from a datagrid to a combobox, I'm doing this way to get the data:

<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" width="388" height="475"     creationComplete="init()" label="{_material.material_id > 0 ? _material.tipo : 'Novo Material'}">

<mx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.managers.PopUpManager;
        import Eventos.MaterialEvent;

        import Eventos.TMaterialEvent;
    import Formularios.TMaterialForm;
    import mx.collections.ArrayCollection;

    [Bindable] private var tMateriais:ArrayCollection;
    // Para criar a conexão com o banco de dados
    private function openDatabase():void
    {
        var file:File = File.applicationDirectory.resolvePath("bd/SISC.db");
        sqlConnection = new SQLConnection();
        sqlConnection.open(file);
        // Para verificar se o banco de dados já existe se sim utiliza-o senão cria um novo, não é útil se não cria a tabela
        //var isNewDB:Boolean = !file.exists;
        //if (isNewDB) createDatabase();

        findAll();
    }

    // Para selecionar a tabela do banco de dados desejada
    private function findAll():void
    {
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = "SELECT tmaterial FROM TMATERIAL";
        stmt.execute();
        tMateriais = new ArrayCollection(stmt.getResult().data);
    }





        [Bindable] public var _material:Object;

        public var sqlConnection:SQLConnection;

        private var validators:Array;

        private function init():void
        {
            validators = [tipoValidator, responsavelValidator, compartimentoValidator];
            openDatabase();// colocado para abrir o banco de outro lugar
        }

        public function set material(material:Object):void
        {
            _material = material;
        }

        public function get material():Object
        {
            return _material;
        }

        private function save():void
        {
            if (Validator.validateAll(validators).length>0)
            {
                return;
            }

            _material.tipo = tipo.text;
            _material.num = num.text;
            _material.responsavel = responsavel.text;
            _material.compartimento = compartimento.text;
            _material.observacoes = observacoes.text;
            if (_material.material_id > 0)
            {
                update();
            }
            else
            {
                insert();
            }
        }

        private function insert():void
        {
            try 
            {
                var stmt:SQLStatement = new SQLStatement();
                stmt.sqlConnection = sqlConnection;
                stmt.text = 
                    "INSERT INTO material (tipo, num, responsavel, compartimento, observacoes) " +
                        "VALUES (:tipo, :num, :responsavel, :compartimento, :observacoes)"; 
                stmt.parameters[":tipo"] = _material.tipo; 
                stmt.parameters[":num"] = _material.num; 
                stmt.parameters[":responsavel"] = _material.responsavel; 
                stmt.parameters[":compartimento"] = _material.compartimento; 
                stmt.parameters[":observacoes"] = _material.observacoes;
                stmt.execute();
                _material.material_id = stmt.getResult().lastInsertRowID;
                label = _material.tipo;
                dispatchEvent(new MaterialEvent(MaterialEvent.CREATE, _material, true));
            }
            catch (error:SQLError)
            {
                Alert.show(error.details, "Erro");
            }
        }

        private function update():void
        {
            try
            {
                var stmt:SQLStatement = new SQLStatement();
                stmt.sqlConnection = sqlConnection;
                stmt.text = 
                    "UPDATE material set " + 
                        "tipo=:tipo, " + 
                        "num=:num, " +
                        "responsavel=:responsavel, " +
                        "compartimento=:compartimento, " +
                        "observacoes=:observacoes " +
                        "WHERE material_id=:materialId";
                stmt.parameters[":tipo"] = _material.tipo; 
                stmt.parameters[":num"] = _material.num; 
                stmt.parameters[":responsavel"] = _material.responsavel; 
                stmt.parameters[":compartimento"] = _material.compartimento; 
                stmt.parameters[":observacoes"] = _material.observacoes; 
                stmt.parameters[":materialId"] = _material.material_id; 
                stmt.execute();
                label = _material.tipo;
                dispatchEvent(new MaterialEvent(MaterialEvent.UPDATE, _material, true));
            }
            catch (error:SQLError)
            {
                Alert.show(error.details, "Error");
            }
        }

        private function deleteItem():void
        {
            try 
            {
                var stmt:SQLStatement = new SQLStatement();
                stmt.sqlConnection = sqlConnection;
                stmt.text = "DELETE FROM material WHERE material_id = :materialId";
                stmt.parameters[":materialId"] = _material.material_id; 
                stmt.execute();
                dispatchEvent(new MaterialEvent(MaterialEvent.DELETE, _material, true));
            }
            catch (error:SQLError)
            {
                Alert.show(error.details, "Erro");
            }
        }

    ]]>
</mx:Script>

<mx:Validator id="tipoValidator" required="true" source="{tipo}" property="text"/>
<mx:Validator id="responsavelValidator" required="true" source="{responsavel}" property="text"/>
<mx:Validator id="compartimentoValidator" required="true" source="{compartimento}" property="text"/>

<mx:Form width="381" height="466">
    <mx:FormItem label="Tipo:" required="true">
        <mx:ComboBox dataProvider="{tMateriais}" id="tipo" text="{_material.tipo}" width="200"/>
    </mx:FormItem>
    <mx:FormItem label="Número:">
        <mx:TextInput id="num" text="{_material.num}" width="200"/>
    </mx:FormItem>
    <mx:FormItem label="Responsavel:" required="true">
        <mx:TextInput id="responsavel" text="{_material.responsavel}" width="200"/>
    </mx:FormItem>
    <mx:FormItem label="Compartimento:" required="true">
        <mx:TextInput id="compartimento" text="{_material.compartimento}" width="200"/>
    </mx:FormItem>
    <mx:FormItem label="Observações:">
        <mx:TextInput id="observacoes" text="{_material.observacoes}" width="200"/>
    </mx:FormItem>

</mx:Form>  

<mx:Button label="Salvar" click="save()" left="16" bottom="20"/> 
<mx:Button label="Deletar" click="deleteItem()" left="87" bottom="20"/> 

but as a result appears in the combobox [object Object] and not the name of the type of material inserted in the table TMATERIAL, if I insert 30 values ​​in Table tmaterial in another form, the 30 appear in combobox as [object Object] I traced this and the error are in this form. Could anyone help me? Sorry the english (google translator). Thnx.

1

1 Answers

1
votes

The problem is the ComboBox needs a hint as to how it should show the name for each item in the dataProvider. By default, many Flex components look for the elements in the dataProvider to have a property named label. If such a property exists, and it is a String, the ComboBox will display the value of that label property.

If the elements in the dataProvider don't have a label property, then the Flex component will call toString() on the dataProvider object which in this case results in the output "[object Object]".

If the elements in your dataProvider do not have a label property, then you can tell the ComboBox how to display the name by using either the labelField or labelFunction properties of the ComboBox.

Use the labelField property to specify the name of the property in your dataProvider that can be used as the label (in your case I believe this is tipo or :tipo)

<mx:ComboBox dataProvider="{tMateriais}" id="tipo" labelField="tipo" />

Or use the labelFunction property specify a function that will be used to generate the label for each item in the dataProvider.

<mx:ComboBox dataProvider="{tMateriais}" id="tipo" labelFunction="myLabelFunction" />

The labelFunction has the following method signature:

private function myLabelFunction(dataProviderItem : Object) : String