×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    12

    How to read and display array records?

    Hi,

    Do you know how I can read and display the stored array records?
    [Note: My addRecord and viewRecord are working in the same class.]

    From what I understand, I should read my array records as shown below:

    try {
    RecordStore fromstore = RecordStore.openRecordStore(REC_STORE, false);
    byte[] data = fromstore.getRecord();

    ByteArrayInputStream bin = new ByteArrayInputStream(data);
    DataInputStream din = new DataInputStream(bin);

    String[] record = new String[din.readInt()];

    for(int i = 0; i < record.length; i++)
    {
    record[i] = din.readUTF();
    }
    din.close();
    fromstore.closeRecordStore();
    }

    But I am not sure if it is correct.
    Please advise me.
    Thanks.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to read and display array records?

    getRecord() needs a record ID.

    closeRecordStore() must go in a finally{} clause, or you risk the record store being left open if an exception occurs. Any record store open when the application terminates may lose data or become corrupt.

    If you stored an array of Strings in the same way (using a ByteArrayOutputStream and DataOutputStream, writing the array length as a int, followed by the Strings using writeUTF()), then yes, this is how you read them back.

    How you display them... is up to you. You can display them in a List, for example.

    Graham.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    12

    Re: How to read and display array records?

    To display an array record, do I use "system.out.printIn"?
    If possible, could you provide a simple coding template for reference?

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to read and display array records?

    Here is a simple MIDlet that displays a list of strings.

    PHP Code:
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.AlertType;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.List;
    import javax.microedition.midlet.MIDlet;

    public class 
    ListView extends MIDlet implements CommandListener {
        
    // this is the "exit" command
        
    private static final Command EXIT_COMMAND = new Command("Exit"Command.EXIT, 0);

        
    // this will refer to the list
        
    private List myList;

        public 
    void startApp() {
            if (
    myList == null) {
                
    // if we don't have a list yet, make one
                
    myList makeList();
            }
            
    // put the list on the screen
            
    Display.getDisplay(this).setCurrent(myList);
        }

        public 
    void pauseApp() {
            
    // do nothing
        
    }

        public 
    void destroyApp(boolean conditional) {
            
    // do nothing
        
    }

        private List 
    makeList() {
            
    // this array will be used in the list
            
    String[] data = new String[] {
                
    "Fish",
                
    "Cow",
                
    "Llama",
                
    "Elephant",
                
    "Banana",
                
    "Luxury yacht",
            };

            
    // make the list
            
    List list = new List("Things", List.IMPLICITdatanull);
            
    // add the "Exit" command to the list
            
    list.addCommand(EXIT_COMMAND);
            
    // make sure that "command" events are sent to this MIDlet
            
    list.setCommandListener(this);
            
            return list;
        }

        public 
    void commandAction(Command cDisplayable d) {
            if (
    == EXIT_COMMAND) {
                
    notifyDestroyed();
            } else if (
    == List.SELECT_COMMAND) {
                
    // find out which item in the list was selected
                
    String selected myList.getString(myList.getSelectedIndex());
                
    // create an "alert" to display a message
                
    Alert a = new Alert("Chosen""You chose " selectednullAlertType.INFO);
                
    // display the alert
                
    Display.getDisplay(this).setCurrent(a);
            }
        }

    Hope it helps.

    Graham.

Similar Threads

  1. Recording in Amr Format
    By losym in forum Symbian Media (Closed)
    Replies: 12
    Last Post: 2009-03-26, 06:41
  2. Read text from display
    By Kaysc in forum Mobile Java General
    Replies: 1
    Last Post: 2008-04-18, 05:09
  3. How to read Name Display Setting?
    By twarna in forum Symbian
    Replies: 3
    Last Post: 2008-02-18, 13:21
  4. read RMS records
    By artemg in forum Mobile Java General
    Replies: 1
    Last Post: 2004-01-10, 22:30

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×