首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何计算RecordStore中的所有记录数

如何计算RecordStore中的所有记录数
EN

Stack Overflow用户
提问于 2013-07-09 21:22:15
回答 3查看 206关注 0票数 1

我有一个LWUIT应用程序,它应该显示LWUIT列表中的记录数。

要获取所有记录,我使用了一个名为getRecordData()的方法,该方法将所有记录作为字符串数组返回,它工作得很好。

但是我如何计算这些记录的数量呢?

代码语言:javascript
复制
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) {
    }
}

我写了这段代码,但它给了NullPointerException at recordStore.enumerateRecords (null, null,true);

所以我认为这里的问题。

请帮帮忙。

代码语言:javascript
复制
myButton.addActionListener( new ActionListener()
{
    public void actionPerformed(ActionEvet av)
    { 
        try
        {
            RecordEnumeration enumeration = recordStore.enumerateRecords (null, null,true); 
            int o =recordStore.getNumRecords () ; 
        } 
        catch(Exception e)
        { 
        } 
    } 
}); 
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-14 08:14:38

这是我的问题的解决方案,我做了一个for循环来获取

数组的元素。

计数器应为数组的长度

代码语言:javascript
复制
    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);

                   }});
票数 0
EN

Stack Overflow用户

发布于 2013-07-18 02:49:43

您需要的是enumeration.numRecords();我认为recordStore.getNumRecords()也应该有效,因为这是您用来填充数组的方法,您甚至可以使用数组本身的长度。这些选项都在代码中,最好多探索一点,并查看文档来解决一些琐碎的问题。

票数 0
EN

Stack Overflow用户

发布于 2013-08-03 05:21:10

您可以使用数组的长度,或者设置记录存储的RecordListener,并在向记录存储添加记录时增加计数器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17549315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档