2013-07-09 41 views
1

我有一個應該顯示LWUIT列表中的記錄數的LWUIT應用程序。我如何統計RecordStore中的所有記錄數

爲了獲得所有記錄,我使用了一種名爲getRecordData()的方法,它將所有記錄作爲字符串數組返回,它工作正常。

但是,如何計算這些記錄的數量?

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);

所以我覺得問題就在這裏。

請大家幫忙。

myButton.addActionListener(new ActionListener() 
{ 
    public void actionPerformed(ActionEvet av) 
    { 
     try 
     { 
      RecordEnumeration enumeration = recordStore.enumerateRecords (null, null,true); 
      int o =recordStore.getNumRecords() ; 
     } 
     catch(Exception e) 
     { 
     } 
    } 
}); 

回答

0

這裏是我的問題的解決方案,我做一個for循環來獲取數組的元素數

計數器應該是數組的長度

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); 

        }}); 
+1

恭喜,您可以刪除for循環它是額外的代碼,並使用numRecord = record.length –

0

你需要的是enumeration.numRecords();我認爲recordStore.getNumRecords()也應該工作,因爲這是你使用填充數組,你甚至可以使用數組本身的長度。這些選項都在代碼中,最好再多探究一下,並檢查文檔以解決微不足道的問題。

+0

不幸enumeration.numRecords()也不會給出所需的結果(同recordStore.getNumRecords()) – JavaFan

+0

我認爲這個問題可能是在你代表你想要保存的數據的方式。唯一的方法是循環遍歷每個要計數的對象,並設置一個計數器,當您看到要計算的對象時,計數器會遞增。 – Ajibola

0

您可以使用數組長度或將RecordListener設置爲您的記錄存儲,並在將記錄添加到記錄存儲時增加計數器。

+0

數組長度解決不了問題String record [] = getRecordData(); '嘗試 { 對(INT J = 0 J。 JavaFan

+0

'recordStore.addRecordListener(新RecordListener(){ 公共無效recordDeleted(javax.microedition.rms.RecordStore RecordStore中,INT C) { } 公共無效recordChanged(RecordStore中的RecordStore,INT的recordId) { } 公共無效recordAdded(RecordStore中的RecordStore,INT的recordId) { System.out的。的println( 「計數器」 +計數器); } } );'**此代碼並沒有要求任何建議工作,請** – JavaFan

+1

我運行代碼,並找到了一些錯誤:1,當您添加記錄的RecordStore它永遠不會打開(首先打開比插入記錄)2 - 如果語句[if(j1 == getRecordData()。length)]永不等於,因爲總是數組長度超過計數器,你應該減去數組的長度[if(j1 == getRecordData()。length-1)]比檢查(你的if塊只添加最後一條記錄到你的列表,如果你想要所有記錄刪除if語句)。 –

相關問題