1. java
  2. android
  3. c#
  4. .net
  5. javascript
  6. php
  7. jquery
  8. html
  9. sql

Por favor me ajudem a fazer o comando relatorio funcionar corretamente?

enter code here
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * @author bruno
 */
public class racaoMIDlet extends MIDlet implements CommandListener{

    private boolean midletPaused = false;
    //FAZER A DECLARAÇÃO DO RMS
    private RecordStore rs = null;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Form tela1;
    private StringItem stringItem;
    private ChoiceGroup racaoGroup;
    private TextField txtValor;
    private Form tela2;
    private StringItem stringItemRelatorio;
    private Command salvarCommand;
    private Command voltarCommand;
    private Command relatorioCommand;

//</editor-fold>//GEN-END:|fields|0|

    /**
     * The racaoMIDlet constructor.
     */
    public racaoMIDlet() {
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initializes the application. It is called only once when the MIDlet is
     * started. The method is called before the
     * <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
        racaoGroup = new ChoiceGroup("Selecione a Ra\u00E7\u00E3o:", Choice.EXCLUSIVE);//GEN-BEGIN:|0-initialize|1|0-postInitialize
        racaoGroup.append("Pedigree 10kg", null);
        racaoGroup.append("Royal Canin 10kg", null);
        racaoGroup.append("Faro 10kg", null);
        racaoGroup.append("C\u00E3o Brother 10kg", null);
        racaoGroup.setSelectedFlags(new boolean[]{false, false, false, false});
        txtValor = new TextField("Valor no Cambu\u00E1:", "", 10, TextField.DECIMAL);
        stringItemRelatorio = new StringItem("Relatório" + "\n\n", "");
        salvarCommand = new Command("Salvar", Command.OK, 0);
        voltarCommand = new Command("Voltar", Command.OK, 0);

        tela2 = new Form("Tela 2", new Item[]{stringItemRelatorio});
        tela2.addCommand(voltarCommand);

        tela2.setCommandListener(this);
        relatorioCommand = new Command("Relat\u00F3rio", Command.OK, 0);
        tela1 = new Form("Tela 1", new Item[]{getStringItem(), racaoGroup, txtValor});
        tela1.addCommand(salvarCommand);
        tela1.addCommand(relatorioCommand);
        tela1.setCommandListener(this);//GEN-END:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction

        switchDisplayable(null, tela1);//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The
     * <code>display</code> instance is taken from
     * <code>getDisplay</code> method. This method is used by all actions in the
     * design for switching displayable.
     *
     * @param alert the Alert which is temporarily set to the display; * *
     * if <code>null</code>, then <code>nextDisplayable</code> is set
     * immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a
     * particular displayable.
     *
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == tela1) {//GEN-BEGIN:|7-commandAction|1|21-preAction
            if (command == relatorioCommand) {//GEN-END:|7-commandAction|1|21-preAction
                //limpa a tela toda vez que for montar o relatório
                stringItemRelatorio.setText("");

                //FAZER A LEITURA DO ARQUIVO RMS, UTILIZAR O ENUMERATE E O COMPARATOR
                //PARA TRATAR E ORDENAR OS DADOS
 try {
       RecordEnumeration enum1 = rs.enumerateRecords(null, comparator, false);

 while(enum1.hasNextElement()){
 byte[] dados = enum1.nextRecord();
 ByteArrayInputStream baos = new ByteArrayInputStream(dados);
 DataInputStream dis = new DataInputStream(baos);

   String racaoSelRegistro = dis.readUTF();
                    float valorSelRegistro = dis.readFloat();
 tela2.append(racaoSelRegistro + "-" + valorSelRegistro);

 }} catch (RecordStoreException ex) {
                    ex.printStackTrace();
                    //será tratado quando for visto
                } catch (IOException ex) {
                    ex.printStackTrace();// write pre-action user code here
                }


                switchDisplayable(null, tela2);//GEN-LINE:|7-commandAction|2|21-postAction
                // write post-action user code here
            } else if (command == salvarCommand) {//GEN-LINE:|7-commandAction|3|17-preAction
                //recupera a opção selecionada e o valor
                String racaoSel = racaoGroup.getString(racaoGroup.getSelectedIndex());
                float valorSel = Float.parseFloat(txtValor.getString());

                //FAZER A GRAVAÇÃO USANDO O RMS. UTILIZE AS DUAS VARIAVEIS ACIMA
                //PARA FAZER A GRAVAÇÃO NO RMS


                //criar a estrutura do string para converter para uma rede byte
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);
                //escrever os dados no stream
                try {

                    dos.writeUTF(racaoSel);
                    dos.writeFloat(valorSel);
                    dos.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }//converter em byte
                byte[] dados = baos.toByteArray();
                //Adicionar em Rs
                try {
                    rs.addRecord(dados, 0, dados.length);

                } catch (RecordStoreException ex) {
                    ex.printStackTrace();

                }


                //limpar a tela
                txtValor.setString("");
//GEN-LINE:|7-commandAction|4|17-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|19-preAction
        } else if (displayable == tela2) {
            if (command == voltarCommand) {//GEN-END:|7-commandAction|5|19-preAction
                // write pre-action user code here
                switchDisplayable(null, tela1);//GEN-LINE:|7-commandAction|6|19-postAction
                // write post-action user code here

            }
                //GEN-BEGIN:|7-commandAction|7|7-postCommandAction
        }//GEN-END:|7-commandAction|7|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|8|
//</editor-fold>//GEN-END:|7-commandAction|8|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initialized instance of stringItem component.
     *
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            stringItem = new StringItem("Controle de Gastos com Ra\u00E7\u00E3o Canina", "");//GEN-LINE:|25-getter|1|25-postInit
            // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return stringItem;
    }
//</editor-fold>//GEN-END:|25-getter|2|

    /**
     * Returns a display instance.
     *
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started. Checks whether the MIDlet have been
     * already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        //FAZER ABERTURA DO RMS
        try {
            rs = RecordStore.openRecordStore("dados2", true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     *
     * @param unconditional if true, then the MIDlet has to be unconditionally
     * terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
RecordComparator comparator = new RecordComparator() {
 public int compare(byte[] rec1, byte[] rec2) {
 DataInputStream dataStreamRec1 = new DataInputStream(new ByteArrayInputStream(rec1));
 DataInputStream dataStreamRec2 = new DataInputStream(new ByteArrayInputStream(rec2));
 try {
 String racao1 = dataStreamRec1.readUTF();
float valor1 = dataStreamRec1.readFloat();
 String racao2 = dataStreamRec2.readUTF();
 float valor2 = dataStreamRec2.readFloat();
 if(valor1 != valor2){
 // operador ternário
 return valor1 < valor2 ? 
 RecordComparator.FOLLOWS :
 RecordComparator.PRECEDES;
 } else { 
 return RecordComparator.EQUIVALENT;
 }
 } catch (IOException ex) {
 ex.printStackTrace();
 }

 return RecordComparator.EQUIVALENT;

 }
 };



}
  • ta faltando coisa ai

    Fernando Beckenbach Buonocore   08 de ago de 2014
  • e qual que é o erro?

    Bruno Rozendo   09 de ago de 2014
  1. Você vai ver essas setas em qualquer página de pergunta. Com elas, você pode dizer se uma pergunta ou uma resposta foram relevantes ou não.
  2. Edite sua pergunta ou resposta caso queira alterar ou adicionar detalhes.
  3. Caso haja alguma dúvida sobre a pergunta, adicione um comentário. O espaço de respostas deve ser utilizado apenas para responder a pergunta.
  4. Se o autor da pergunta marcar uma resposta como solucionada, esta marca aparecerá.
  5. Clique aqui para mais detalhes sobre o funcionamento do GUJ!

0 resposta

Não é a resposta que estava procurando? Procure outras perguntas com as tags java ou faça a sua própria pergunta.