Jump to content


Photo

Vetor-Exibir Dados


  • Faça o login para participar
Nenhuma resposta neste tópico

#1 andretva

andretva

    Novato no fórum

  • Usuários
  • 1 posts
  • Sexo:Masculino
  • Localidade:SP

Posted 25/10/2011, 10:41

No programa abaixo eu cadastro um cliente informando todos os dados, depois clico em salvar, depois clico em voltar e mano listar os clientes cadastrados.
Os cliente são cadastratos todos em um vetor.

Mais
Tenho uma duvida, eu preciso exibir os dados preenchidos do vetor cliente utilizando a função exibirdados, quando eu clico no listar e no nome do cliente cadastrado.
vai abaixo o codigo fonte.



import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Date;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

/**
* @author Aluno
*/
public class Clientes extends MIDlet implements CommandListener,ItemStateListener{
private Display display;
private Form form;
private TextField txtNome;
private Command cmdSair;
private DateField dfNascimento;
private ChoiceGroup cgSexo;
private ChoiceGroup cgConh;
private List lstMenu;
private Command cmdVoltar;
private Ticker ticker;
private Alert alerta;
private Command cmdSalvar;
private List resultados;
public void startApp() {

display=Display.getDisplay(this);
form=new Form("Cadastro de Clientes");
txtNome=new TextField("Nome","",50,TextField.ANY);
form.append(txtNome);
cmdSalvar=new Command("Salvar",Command.SCREEN,3);
form.addCommand(cmdSalvar);
cmdVoltar=new Command("Voltar",Command.BACK,2);
form.addCommand(cmdVoltar);
form.setCommandListener(this);
dfNascimento=new DateField("Nascimento",DateField.DATE);
dfNascimento.setDate(new Date());
form.append(dfNascimento);
form.setItemStateListener(this);
Gauge gNota=new Gauge("Nota",true,10,1);
form.append(gNota);
cgSexo=new ChoiceGroup("Sexo", Choice.EXCLUSIVE);
form.setItemStateListener(this);
int selected=cgSexo.append("Masculino",null);
cgSexo.append("Feminino",null);
cgSexo.setSelectedIndex(selected, true);
form.append(cgSexo);
cgConh=new ChoiceGroup("Conhecimento",Choice.MULTIPLE);
cgConh.append("Java",null);
cgConh.append("PHP",null);
form.append(cgConh);
ticker=new Ticker("Obrigado por utilizar nosso Sistema");

lstMenu=new List("Clientes",List.IMPLICIT);
lstMenu.append("Listar",null);
lstMenu.append("Cadastrar",null);
cmdSair=new Command("Sair",Command.EXIT,1);
lstMenu.addCommand(cmdSair);


lstMenu.setCommandListener(this);
lstMenu.setTicker(ticker);

display.setCurrent(lstMenu);
try{
Image img=Image.createImage("/ninja.jpeg");
alerta=new Alert("Alerta","Menssagem",img,AlertType.ALARM);
display.setCurrent(alerta);
}catch(Exception ex){}



}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable s) {
if(c==cmdSair){
destroyApp(false);
notifyDestroyed();
}
else if(c==cmdVoltar){
display.setCurrent(lstMenu);
}
else if(c==cmdSalvar){
incluiRegistro();
}
if(c==List.SELECT_COMMAND){
if(lstMenu.getSelectedIndex()==0){
listaRegistros();
}

else{
display.setCurrent(form);
}



}
else if(c==List.SELECT_COMMAND){
int indice=resultados.getSelectedIndex();
exibeDados(resultados.getString(indice));
}

}

public void itemStateChanged(Item item) {
if(item==cgSexo){
int index=cgSexo.getSelectedIndex();
System.out.println(cgSexo.getString(index));
}
}

private void incluiRegistro() {
try{
RecordStore rs=RecordStore.openRecordStore("Clientes",true);

String nome=txtNome.getString();
Date nascimento=dfNascimento.getDate();

String sexo=cgSexo.getString(cgSexo.getSelectedIndex());

ByteArrayOutputStream array=new ByteArrayOutputStream();
DataOutputStream data=new DataOutputStream(array);

data.writeUTF(nome);
data.writeLong(nascimento.getTime());
data.writeUTF(sexo);

rs.addRecord(array.toByteArray(), 0,array.size());
rs.closeRecordStore();
}catch(Exception ex){}
}

private void listaRegistros() {
try{
resultados=new List("Resultado",Choice.IMPLICIT);

RecordStore rs=RecordStore.openRecordStore("Clientes", false);
RecordEnumeration re=rs.enumerateRecords(null, null, false);


while(re.hasNextElement()){
ByteArrayInputStream array=new ByteArrayInputStream(re.nextRecord());
DataInputStream data= new DataInputStream(array);

String nome=data.readUTF();
resultados.append(nome,null);

data.close();
array.close();

}
rs.closeRecordStore();
display.setCurrent(resultados);

}catch(Exception ex){
System.out.println(ex.getMessage());
}
}

private void exibeDados(String nome) {
try{


RecordStore rs=RecordStore.openRecordStore("Clientes", false);
RecordEnumeration re=rs.enumerateRecords(null, null, false);


while(re.hasNextElement()){
ByteArrayInputStream array=new ByteArrayInputStream(re.nextRecord());
DataInputStream data= new DataInputStream(array);

nome=data.readUTF();



****Foi aqui que eu fiquei parado e não consegui prosseguir*******
****Como que faço para pegar os dados do cliente e exibilos na tela ???*******
//if(){


//}





rs.closeRecordStore();
display.setCurrent(resultados);
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}

}
}




0 user(s) are reading this topic

0 membro(s), 0 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq