1

I have a LWUIT app that should display the number of records in a LWUIT list.

To get all the records I use a method called getRecordData() that returns all records as a String array, it works fine.

But how do I count the number of these records?

import java.util.*;
import com.sun.lwuit.events.*;
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.plaf.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms .*;
public class number_of_records extends MIDlet {
    private RecordStore recordStore;
    // Refresh2( ) method for getting the time now
    public String Refresh2()
    {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        Date myDate = new Date();
        calendar.setTime(myDate);
        StringBuffer time = new StringBuffer();
        time.append(calendar.get(java.util.Calendar.HOUR_OF_DAY)).append(':');
        time.append(calendar.get(java.util.Calendar.MINUTE)) ;
        // time.append(calendar.get(java.util.Calendar.SECOND));
        String tt = time.toString();
        return tt;
    }
    // return all records of recordStore RecordStore
    public String [] getRecordData( )
    {
        String[] str = null;
        int counter = 0;
        try
        {
            RecordEnumeration enumeration = recordStore.enumerateRecords(null, null, false);
            str = new String[recordStore.getNumRecords()];
            while(enumeration.hasNextElement())
            {
                try
                {
                    str[counter] = (new String(enumeration.nextRecord()));
                    counter ++;
                }
                catch(javax.microedition.rms.RecordStoreException e)
                {
                }
            }
        }
        catch(javax.microedition.rms.RecordStoreNotOpenException e)
        {
        }
        catch(java.lang.NullPointerException n)
        {
        }
        return str;
    }
    public void startApp()
    {
        com.sun.lwuit.Display.init(this);
        final Button addition = new Button("add a goal");
        final com.sun.lwuit.TextField tf = new com.sun.lwuit.TextField();
        final com.sun.lwuit.List mylist = new com.sun.lwuit.List();
        final Button All = new Button("All Goals");
        final com.sun.lwuit.Form ff = new com.sun.lwuit.Form();
        final com.sun.lwuit.Form g = new com.sun.lwuit.Form();
        ff.getStyle().setBgColor(0X99CCFF);
        All.getStyle().setBgColor(0X0066CC);
        Style g_style5 = g.getSelectedStyle() ;
        g.addComponent(tf);
        g.addComponent(addition);
        addition.getStyle().setBgColor(0X0066CC);
        g.addComponent(All);
        g.getStyle().setBgColor(0X99CCFF);
        addition.addActionListener(new ActionListener()
        { 
            public void actionPerformed(ActionEvent ae)
            {
                //
                String s =tf.getText();
                if( s!=null && s.length() > 0)
                {
                    try
                    {
                        // Store the time in the String k
                        String k = Refresh2();
                        // The record and the time stored in KK String
                        String kk =tf.getText()+"-"+k;
                        // Add an item (the kk String) to mylist List.
                        mylist.addItem(kk);
                        byte bytestream[] = kk.getBytes() ;
                        // Add a record to recordStore.
                        int i = recordStore.addRecord(bytestream, 0, bytestream.length);
                    }
                    catch(Exception ex) { }
                    // Inform the User that he added the a record.
                    Dialog validDialog = new Dialog(" ");
                    Style Dialogstyle = validDialog.getSelectedStyle() ;
                    validDialog.setScrollable(false);
                    validDialog.getDialogStyle().setBgColor(0x0066CC);
                    validDialog.setTimeout(1000); // set timeout milliseconds
                    TextArea textArea = new TextArea("...."); //pass the alert text here
                    textArea.setFocusable(false);
                    textArea.setText("A goal has been added"+"" );
                    validDialog.addComponent(textArea);
                    validDialog.show(0, 10, 10, 10, true);
                }
                // Information to user that he/she didn’t add a record
                else if((s==null || s.length()<= 0))
                {
                    Dialog validDialo = new Dialog(" ");
                    validDialo.setScrollable(false);
                    validDialo.getDialogStyle().setBgColor(0x0066CC);
                    validDialo.setTimeout(5000); // set timeout milliseconds
                    TextArea textArea = new TextArea("...."); //pass the alert text here
                    textArea.setFocusable(false);
                    textArea.setText("please enter scorer name or number");
                    validDialo.addComponent(textArea);
                    validDialo.show(50, 50, 50, 50, true);
                }
            }
        });
        /*Action here for displaying all records of recordStore RecordStore in a new form */
        All.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                try
                {
                    recordStore = RecordStore.openRecordStore("My Record Store", true);
                }
                catch(Exception ex) {} 
                try
                {
                    com.sun.lwuit.Label l = new com.sun.lwuit.Label(" Team Goals") ;
                    ff.addComponent(l);
                    // Store the records of recordStore in string array
                    String [] record= getRecordData();
                    int j1;
                    String valueToBeInserted2="";
                    int k=getRecordData().length;
                    for( j1=0;j1< getRecordData().length;j1++)
                    {
                        valueToBeInserted2=valueToBeInserted2 + " " + record[j1];
                        if(j1==getRecordData().length)
                        {
                            mylist.addItem(record[j1]);
                            int m = getRecordData().length;
                            // Counting the number of records
                            String goals =""+getRecordData().length;
                            /* I tried to use for…loop to count them by length of the recordStore and render it.
                               This list also should display the number of records on the form.
                               But it didn’t !!!
                            */
                            mylist.addItem(goals);
                        }
                    }
                    ff.addComponent(mylist);
                }
                catch(java.lang.IllegalArgumentException e)
                {
                }
                finally
                {
                    ff.show();
                }
            }
        }
        );
        g.show();
    }
    public void pauseApp()
    {
    }
    public void destroyApp(boolean unconditional) {
    }
}

I Wrote this code but it gives NullPointerException at recordStore.enumerateRecords (null, null,true);

So I think the problem here.

please help.

myButton.addActionListener( new ActionListener()
{
    public void actionPerformed(ActionEvet av)
    { 
        try
        {
            RecordEnumeration enumeration = recordStore.enumerateRecords (null, null,true); 
            int o =recordStore.getNumRecords () ; 
        } 
        catch(Exception e)
        { 
        } 
    } 
}); 
Miklos Aubert
  • 4,405
  • 2
  • 24
  • 33
PHPFan
  • 756
  • 3
  • 12
  • 46

3 Answers3

0

what you need is enumeration.numRecords(); i reckon recordStore.getNumRecords() should work also, since this is what you are using the populate the array, you could even use the length of the array itself. These options are all in the code, it would be better to explore a bit more and also check the documentation to resolve trivial problems.

Ajibola
  • 1,218
  • 16
  • 28
  • unfortunately enumeration.numRecords( ) also wouldn't gives the required result (same as recordStore.getNumRecords() ) – PHPFan Jul 31 '13 at 10:21
  • i think the issue might be in the way you are representing the data that you want to be saved. The only other way is to loop through each object you want to count and set a counter that you increment when you see the object that you want counted. – Ajibola Jul 31 '13 at 18:50
0

you could use the length of the array or set a RecordListener to your recordstore and increase a counter when added a record to recordstore.

  • length of the array wouldn't solve the problem String record[] = getRecordData(); `try { for(int j=0;j – PHPFan Aug 03 '13 at 11:24
  • `recordStore.addRecordListener(new RecordListener(){ public void recordDeleted( javax.microedition.rms.RecordStore recordStore, int c) { } public void recordChanged(RecordStore recordStore, int recordId) { } public void recordAdded(RecordStore recordStore, int recordId) { System.out.println("Counter"+counter); } } );` **This code didn't work as required any suggestion please** – PHPFan Aug 04 '13 at 13:28
  • 1
    I run your code and find some error: 1- when you add record to recordStore it never opened(first open that than insert record) 2- if statement [ if(j1==getRecordData().length) ] never equals because always length of array more than a counter, you should minus length of array [if(j1==getRecordData().length-1)] than check that(your if block only add last record to your list, if you want all records remove if statement). – Zabihullah Alipour Aug 08 '13 at 16:33
  • The mistake I did is that , I didn't open recordstore when adding a new record. – PHPFan Aug 13 '13 at 23:35
0

here is the solution of my problem , I do a for loop to get the number of

elements of the array.

the counter should be the length of array

    count.addActionListener( new ActionListener()
    {
    public void actionPerformed(ActionEvent av)
   {

            try
           {

          recordStore = RecordStore.openRecordStore("recordStore", true);

            }
            catch(Exception e)

            { }

            try
             {

        RecordEnumeration enumeration = recordStore.enumerateRecords (null,  null,true);

               }
             catch(Exception e)
             {

             }
            String record[] = getRecordData();
            int j;


             j =   record.length-1;

                Dialog validDialog = new Dialog(" ");
                Style Dialogstyle = validDialog.getSelectedStyle() ;
                validDialog.setScrollable(false);
                validDialog.getDialogStyle().setBgColor(0x0066CC);
                validDialog.setTimeout(1000); // set timeout milliseconds
                TextArea textArea = new TextArea("....");  
                textArea.setFocusable(false);
                textArea.setText("Number Counted"+j );
                validDialog.addComponent(textArea);
                validDialog.show(0, 10, 10, 10, true);

                   }});
PHPFan
  • 756
  • 3
  • 12
  • 46