Erro Alert

8 respostas
J

Estou com um problema neste codigo: a aplicação nao apresenta a tela Alert, alguem sabe o que pode ser???

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Cadastro extends MIDlet implements CommandListener{
    static final Command EXIT_CMD = new Command("Sair", Command.EXIT, 1);
    static final Command BACK_CMD = new Command("Voltar", Command.BACK, 1);
    static final Command CADASTRAR_CMD = new Command("Cadastrar", Command.OK, 2);
    Display display;
    List listMain;
    Form fmcadastro;
    Alert alertCampos;
    String[] infCadastro;
    TextField tfNome, tfEnd, tfNum, tfBairro, tfComplemento, tfemail, tfTel, tfCidade;
    
    public Cadastro() {
        display = Display.getDisplay(this);
        
        //Lista-menu principal
        String[] opcoes = {"1. Cardápio", "2. Cadastro" };
        listMain = new List(welcome + " >Início", List.IMPLICIT, opcoes,null);
        
        //Tela Cadastro
        fmcadastro = new Form("Cadastro");
        fmcadastro.addCommand(BACK_CMD);
        fmcadastro.addCommand(CADASTRAR_CMD);
        
        tfNome = new TextField( "Nome: " , "" , 30, TextField.ANY);
        tfEnd = new TextField( "Endereço: " , "" , 30, TextField.ANY);
        tfNum = new TextField( "Número: " , "" , 10, TextField.NUMERIC);
        tfBairro = new TextField( "Bairro: " , "" , 30, TextField.ANY);
        tfComplemento = new TextField( "Complemento: " , "" , 30, TextField.ANY);
        tfemail = new TextField( "E-mail: " , "" , 30, TextField.EMAILADDR);
        tfTel = new TextField( "Tel/Cel: " , "" , 30, TextField.NUMERIC);
        tfCidade = new TextField( "Cidade: " , "" , 30, TextField.ANY);
        
        tfemail.setString("@");
        
        fmcadastro.append(tfNome);
        fmcadastro.append(tfEnd);
        fmcadastro.append(tfNum);
        fmcadastro.append(tfBairro);
        fmcadastro.append(tfComplemento);
        fmcadastro.append(tfCidade);
        fmcadastro.append(tfTel);
        fmcadastro.append(tfemail);    
        
        //Alerta: Campos completos na tela cadastro
        alertCampos = new Alert("Campos Incompletos", "Confira todos os campos e preencha corretamente.", null, null);
        //alertCampos.setTimeout(5000);
        
        listMain.setCommandListener(this);
        fmcadastro.setCommandListener(this);
        alertCampos.setCommandListener(this);
    }
    
    public void startApp() {
        display.setCurrent(listMain);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == EXIT_CMD) {
            destroyApp(true);
            notifyDestroyed();
        }else if (c == List.SELECT_COMMAND) {
                if (listMain.getSelectedIndex() == 1){
                    display.setCurrent(fmcadastro);
                }
                else{
                    //display.setCurrent(new Menu()); //lista de pizzas
                }
        }else if (c == BACK_CMD)
                display.setCurrent(listMain);
        else if (c == CADASTRAR_CMD) {
            if (info(tfNome, tfEnd, tfNum, tfBairro, tfComplemento, tfCidade, tfemail, tfTel))
                display.setCurrent(listMain);
            else
                display.setCurrent(alertCampos);
        }
    }

    boolean info(TextField nome, TextField end, TextField num, TextField bairro, TextField complemento, TextField cidade, TextField email, TextField tel) {
        if (nome.getString() == "" || end.getString() == "" || num.getString() == "" || 
                bairro.getString() == "" || complemento.getString() == "" || 
                cidade.getString() == ""||  tel.getString() == "") //email.getString() == "@" ||
            return false;
        else{
            String[] infCadastro = {nome.getString(), end.getString(), num.getString(), bairro.getString(), complemento.getString(), cidade.getString(), email.getString(), tel.getString()};
            return true;
        }
    }
}

8 Respostas

godinez

Tente criar o Alert quando um comando for chamado, e nao no construtor.
Exemplo:

else if (c == CADASTRAR_CMD) {   
            if (info(tfNome, tfEnd, tfNum, tfBairro, tfComplemento, tfCidade, tfemail, tfTel))   
                display.setCurrent(listMain);   
            else
                alertCampos = new Alert("Campos Incompletos", "Confira todos os campos e preencha corretamente.", null, null);   
                alertCampos.setTimeout(5000);   
                display.setCurrent(alertCampos, this);

ateh

Reilander

acho que onde o alert eh construido nao tem nada haver nao.
detalhe mais a situacao em que voce quer ver o alert

J

Eu quero que seja mostrado no if....caso a expressão seja tru mostre uma tela, caso nao mostre outra. A tela corrente é um cadastro com varias textfield se alguma estiver em branco.. assim que o usuario clicar num comando se nao estiver completo os dados a aplicação abre a tela ALERT mantem por alguns segundos e volta para o cadastro. Fiz varias modificações e nao deram certo??? alguem sabe o que pode ser???

if (c == BACK_CMD)  
display.setCurrent(listMain);  
	else if (c == CADASTRAR_CMD) {  
			if (info(tfNome, tfEnd, tfNum, tfBairro, tfComplemento, tfCidade, tfemail, tfTel))  
				display.setCurrent(listMain);  
			else  
				display.setCurrent(alertCampos);  
}  

boolean info(TextField nome, TextField end, TextField num, TextField bairro, TextField complemento, TextField cidade, TextField email, TextField tel) {  
	if (nome.getString() == "" || end.getString() == "" || num.getString() == "" || bairro.getString() == "" || complemento.getString() == "" || cidade.getString() == ""||  tel.getString() == "")
		return false;  
	else{  
		String[] infCadastro = {nome.getString(), end.getString(), num.getString(), bairro.getString(), complemento.getString(), cidade.getString(), email.getString(), tel.getString()};  
		return true;  
        }  
    }
godinez

Fala ae, blza?
Bom, pelo o que eu vi, seu metodo info() esta sempre retornando true, fiz uma modificacao e funcionou:

boolean info() {   
        if (tfNome.getString().equals("") || tfEnd.getString().equals("")) { 
           return false;   
        } else{   
            String[] infCadastro = {tfNome.getString()};   
            return true;   
        }   
    }

Voce tambem deve setar um timeout pro Alerta:

E retirar a linha alertCampos.setCommandListener(this);

Abraço!

Reilander

qual a finalidade dessa linha 5,
“String[] infCadastro = {tfNome.getString()};” ?

P

Reilander:
qual a finalidade dessa linha 5,
“String[] infCadastro = {tfNome.getString()};” ?

Um array/vetor de um único elemento !

Reilander

e depois o metodo acaba.
o que se faz com ela, da trabalho pro gc?

J

maravilha… é isso mesmo meu amigo Godinez!!!

Na verdade é só um controle de campos obrigatorios. Uma tela da cadastro para que o usuario informe todos os campos (sem nenhum em branco).

Abraço
Ate mais.

Joao

Criado 23 de abril de 2008
Ultima resposta 26 de abr. de 2008
Respostas 8
Participantes 4