Java me 退出应用程序后恢复recordstore

Java me 退出应用程序后恢复recordstore,java-me,lwuit,midlet,rms,lwuit-list,Java Me,Lwuit,Midlet,Rms,Lwuit List,我已经编写了一个LWUIT应用程序,它涉及两个recordStore recordStore 及 唱片店2 用于检索的所有记录的两种方法getRecordData recordStore,方法getRecordData2,用于检索 唱片店2 两个单选按钮rb和rb2被添加到ButtonGroup,称为group 一个名为tf的文本字段 用于为recordstore或recordstore2添加记录的添加按钮 选择rb或rb2后 必须作为记录写入tf TextField的文本 两个列表mylist

我已经编写了一个LWUIT应用程序,它涉及两个recordStore recordStore

唱片店2

用于检索的所有记录的两种方法getRecordData

recordStore,方法getRecordData2,用于检索

唱片店2

两个单选按钮rb和rb2被添加到ButtonGroup,称为group

一个名为tf的文本字段

用于为recordstore或recordstore2添加记录的添加按钮

选择rb或rb2后

必须作为记录写入tf TextField的文本

两个列表mylist和mylist2

mylist以显示recordstore的所有记录

mylist2显示recordstore2的所有记录

“全部”按钮以显示recordstore和recordstore2的所有记录

我面临一个简单的问题:-

退出应用程序后,返回应用程序并按All按钮

mylist和mylist2为空(未显示任何记录)

有人能帮我吗

    import java.util.*;

    import com.sun.lwuit.table.*;

    import com.sun.lwuit.table.DefaultTableModel;

    import javax.microedition.lcdui.*;

    import javax.microedition.midlet.*;

    import javax.microedition.rms.RecordStore;

    import javax.microedition.rms.RecordStoreNotOpenException;

    import javax.microedition.rms .*;

    import com.sun.lwuit.layouts.*;

    import com.sun.lwuit.*;

    import javax.microedition.lcdui.Canvas;


    import com.sun.lwuit.events.*;

    import com.sun.lwuit.plaf.*;

    import javax.microedition.midlet.*;

    import com.sun.lwuit.geom.*;

    import com.sun.lwuit.*;

    import com.sun.lwuit.events.*;

    public class HelloLWUITMidlet3 extends MIDlet implements   Runnable, ActionListener

         {

         private RecordStore recordStore,recordStore2;


           String team,team2;

     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 (Exception e)

                    {

                      e.printStackTrace();

                      }
                            }
                                } 

              catch (Exception e) 

                                   {

                       e.printStackTrace();

                           }


                            return str;
    }

     public String [] getRecordData2()

          {

        String[] str = null;

        int counter = 0;

        try

        {

            RecordEnumeration enumeration = recordStore2.enumerateRecords(null, null, false);

            str = new String[recordStore2.getNumRecords()];

            while(enumeration.hasNextElement())

            {
                try

                {
                    str[counter] = (new String(enumeration.nextRecord()));

                    counter ++;
                }

                catch (Exception e)

                {

                    e.printStackTrace();
                }

            }

        } 

        catch (Exception e) 

                {

            e.printStackTrace();

        }

        return str;

    }

        public void startApp()


        {

          com.sun.lwuit.Display.init(this);


    int bgColor =0x0066CC;

    final ButtonGroup group = new ButtonGroup();

    final  RadioButton rb = new RadioButton("First Team");

    final  RadioButton rb2 = new RadioButton("Second Team");

    group.add(rb);

    group.add(rb2);


    final  f1 g = new f1();

     final com.sun.lwuit.List mylist =  new com.sun.lwuit.List();

    final com.sun.lwuit.List mylist2 =  new com.sun.lwuit.List();


    final form6 my = new form6();

     final  com.sun.lwuit.Form f = new com.sun.lwuit.Form("Football");

       f.getStyle().setBgColor(0X99CCFF);

      BoxLayout boxlayout =new BoxLayout(BoxLayout.X_AXIS);


       final  Button goals = new Button("See goals");

       final  Button button = new Button("Goals Record");

      final    com.sun.lwuit.TextField tf =new com.sun.lwuit.TextField();



     final    com.sun.lwuit.TextField  txtfield5 =new com.sun.lwuit.TextField();


      final    com.sun.lwuit.TextField  txtfield6 =new com.sun.lwuit.TextField();



    final Button addition = new Button("Add Goals");

    final Button All = new Button("All Goals");

    addition.getStyle().setBgColor(0X0066CC);

       com.sun.lwuit.Command back = new com.sun.lwuit.Command("Main_Screen");

    final form6 ff = new form6();

      button.getStyle().setBgColor(0X0066CC);

    goals.getStyle().setBgColor(0X0066CC);

      All.getStyle().setBgColor(0X0066CC);
    Style g_style5 = g.getSelectedStyle() ;

    f.addComponent(button);

    ff.addCommand(back);

     //g.addComponent(main_screen);
    g.addComponent(tf);
    g.addComponent(addition);
     g.addComponent(rb);
     g.addComponent(rb2);


    g.addComponent(All);

     g.getStyle().setBgColor(0X99CCFF);

    g.addCommand(back);

     g.addCommandListener(new ActionListener()

     {


      public void actionPerformed(ActionEvent ae) {



    f.show();
         }



     }
              );


       ff.addCommandListener(new ActionListener()

     {


      public void actionPerformed(ActionEvent ae) {



    f.show();
         }



     }
              );

    ff.getStyle().setBgColor(0X99CCFF);





     try{
             recordStore2 = RecordStore.openRecordStore("My Record Store2", true);
            }
      catch(Exception ex)
         { }
     try
     {
             recordStore = RecordStore.openRecordStore("My Record Store", true);
            }
         catch(Exception ex)
         { }

          All.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent ae) {

    try

    {
        com.sun.lwuit.Label l = new com.sun.lwuit.Label("First Team Goals") ;

       ff.addComponent(l);

       String [] record=  getRecordData();

    int j1;

    String valueToBeInserted2="";


    for( j1=0;j1< getRecordData().length;j1++)

      {


    valueToBeInserted2=valueToBeInserted2 + " " + record[j1];


    if(j1==getRecordData().length)

      {

   mylist.addItem(record[j1]);

      }



    }


    ff.addComponent(mylist);

     com.sun.lwuit.Label ll = new com.sun.lwuit.Label("Second Team Goals") ;

      ff.addComponent(ll);


       String [] record2=  getRecordData2();

    int j2;

    String valueToBeInserted="";


  for( j1=0;j1< getRecordData().length;j1++)

  {


valueToBeInserted2=valueToBeInserted2 + " " + record[j1];


if(j1==getRecordData().length)

                {

        mylist.addItem(record[j1]);



             }

             }


    ff.addComponent(mylist2);

          }
    catch(java.lang.IllegalArgumentException e)


         {

         }

    finally

         {
    ff.show();

          }
             }



      }

    );

     button.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent ae) {


    g.show();

         }


          }

             );

    goals.addActionListener(new ActionListener()

        {

      public void actionPerformed(ActionEvent ae) {



         }

      }
    );





    final  Thread th = new Thread(this);







    addition.addActionListener(new ActionListener()

    {


      public void actionPerformed(ActionEvent ae) {

    String s =tf.getText();

    if(rb.isSelected())

    {



    if(s!=null && s.length() > 0)

    {

     try{

          String kk =tf.getText();

          mylist.addItem(kk);


                 byte bytestream[] = kk.getBytes()  ;

                 int i = recordStore.addRecord(bytestream, 0, bytestream.length);

         }

     catch(Exception ex) 

                 { }


    Dialog validDialog = new Dialog(" ");


    Style Dialogstyle = validDialog.getSelectedStyle() ;

            validDialog.setScrollable(false);


     validDialog.getDialogStyle().setBgColor(0x0066CC);

            validDialog.setTimeout(1000); 

            TextArea textArea = new TextArea("...."); 


            textArea.setFocusable(false);

           textArea.setText("A new goal has been added"+"" );

            validDialog.addComponent(textArea);


            validDialog.show(0, 10, 10, 10, true);




    }

          }


     else if (rb2.isSelected())
    {


    if(s!=null && s.length() > 0)

    {

     try{

          String kk =tf.getText();


          mylist2.addItem(kk);



           byte bytestream[] = kk.getBytes()  ;

             int i = recordStore2.addRecord(bytestream, 0, bytestream.length);

         }

     catch(Exception ex) { }


    Dialog validDialog = new Dialog(" ");

    Style Dialogstyle = validDialog.getSelectedStyle() ;

            validDialog.setScrollable(false);

     validDialog.getDialogStyle().setBgColor(0x0066CC);

            validDialog.setTimeout(1000); 

            TextArea textArea = new TextArea("....");  

            textArea.setFocusable(false);

           textArea.setText("A new Goal has been added"+"" );

            validDialog.addComponent(textArea);

            validDialog.show(0, 10, 10, 10, true);



    }

          }

    else if((rb.isSelected()==false&&rb.isSelected()==false)||(s==null && s.length()<= 0))

    {

        Dialog validDialo = new Dialog(" ");


            validDialo.setScrollable(false);


    validDialo.getDialogStyle().setBgColor(0x0066CC);


            validDialo.setTimeout(5000);  


            TextArea textArea = new TextArea("....");  


            textArea.setFocusable(false);


           textArea.setText("please enter scorer name and choose his team");


            validDialo.addComponent(textArea);





            validDialo.show(50, 50, 50, 50, true);


            txtfield6.clear();


     }



        }

    }

    );





          f.show();



        }

    public void run ()

          {

       while(true)

         {


            String get[] = getRecordData();


             Dialog validDialog = new Dialog(" ");


             Style Dialogstyle = validDialog.getSelectedStyle() ;


            validDialog.setTimeout(5000);


            validDialog.setScrollable(true);



            validDialog.setLayout(null);


            validDialog.setTimeout(5000);

             TextArea textArea = new TextArea( );


    for(int ii=0;ii<getRecordData().length;ii++ )



    {


            textArea.setColumns(getRecordData().length);


            validDialog.addComponent(textArea);



            validDialog.show(10, 10, 10, 10, true);



               System.out.println(get[ii]+"\n");


             System.out.println(textArea.getColumns()+"\n");


                      }


                            }


                                }
        public void pauseApp()


                          {

                           }

        public void destroyApp(boolean unconditional)

                                           { 

                                           }

        public void actionPerformed(ActionEvent ae) 

          {


           notifyDestroyed();

           }

         public  class f1 extends   com.sun.lwuit.Form


      {

            com.sun.lwuit.TextField tf;


               public f1()


              {


      com.sun.lwuit.Form f1 = new com.sun.lwuit.Form("Goals Records");


        tf =new com.sun.lwuit.TextField();


              }






        }



    public  class form6   extends   com.sun.lwuit.Form


      {

    com.sun.lwuit.TextField txtfield3;

    com.sun.lwuit.TextField tf3;


       public form6()

              {

      com.sun.lwuit.Form mylist = new com.sun.lwuit.Form("Goals List");


              }




    }

}
import java.util.*;
导入com.sun.lwuit.table.*;
导入com.sun.lwuit.table.DefaultTableModel;
导入javax.microedition.lcdui.*;
导入javax.microedition.midlet.*;
导入javax.microedition.rms.RecordStore;
导入javax.microedition.rms.RecordStoreNotOpenException;
导入javax.microedition.rms.*;
导入com.sun.lwuit.layouts.*;
导入com.sun.lwuit.*;
导入javax.microedition.lcdui.Canvas;
导入com.sun.lwuit.events.*;
导入com.sun.lwuit.plaf.*;
导入javax.microedition.midlet.*;
导入com.sun.lwuit.geom.*;
导入com.sun.lwuit.*;
导入com.sun.lwuit.events.*;
公共类HelloLWUITMidlet3扩展了MIDlet实现可运行的ActionListener
{
私有记录存储记录存储,记录存储2;
弦乐队,第二队;
公共字符串[]getRecordData()
{
字符串[]str=null;
int计数器=0;
尝试
{
RecordEnumeration枚举=recordStore.enumerateRecords(null、null、false);
str=新字符串[recordStore.getNumRecords()];
while(枚举.hasNextElement())
{
尝试
{
str[counter]=(新字符串(enumeration.nextRecord());
计数器++;
}
捕获(例外e)
{
e、 printStackTrace();
}
}
} 
捕获(例外e)
{
e、 printStackTrace();
}
返回str;
}
公共字符串[]getRecordData2()
{
字符串[]str=null;
int计数器=0;
尝试
{
RecordEnumeration枚举=recordStore2.enumerateRecords(null、null、false);
str=新字符串[recordStore2.getNumRecords()];
while(枚举.hasNextElement())
{
尝试
{
str[counter]=(新字符串(enumeration.nextRecord());
计数器++;
}
捕获(例外e)
{
e、 printStackTrace();
}
}
} 
捕获(例外e)
{
e、 printStackTrace();
}
返回str;
}
公开作废startApp()
{
com.sun.lwuit.Display.init(this);
int bgColor=0x0066CC;
最终按钮组=新按钮组();
最终RadioButton rb=新RadioButton(“第一团队”);
最终RadioButton rb2=新RadioButton(“第二团队”);
加组(rb);
加组(rb2);
最终f1 g=新f1();
final com.sun.lwuit.List mylist=new com.sun.lwuit.List();
final com.sun.lwuit.List mylist2=new com.sun.lwuit.List();
最终表格6 my=新表格6();
最终com.sun.lwuit.Form f=新com.sun.lwuit.Form(“足球”);
f、 getStyle().setBgColor(0X99CCFF);
BoxLayout BoxLayout=新的BoxLayout(BoxLayout.X_轴);
最终按钮目标=新按钮(“见目标”);
最终按钮=新按钮(“目标记录”);
final com.sun.lwuit.TextField=new com.sun.lwuit.TextField();
final com.sun.lwuit.TextField txtfield5=新com.sun.lwuit.TextField();
final com.sun.lwuit.TextField txtfield6=新com.sun.lwuit.TextField();
最终按钮添加=新按钮(“添加目标”);
最终按钮全部=新按钮(“所有目标”);
addition.getStyle().setBgColor(0X0066CC);
com.sun.lwuit.Command back=新的com.sun.lwuit.Command(“主屏幕”);
最终表格6 ff=新表格6();
button.getStyle().setBgColor(0X0066CC);
goals.getStyle().setBgColor(0X0066CC);
All.getStyle().setBgColor(0X0066CC);
Style g_style5=g.getSelectedStyle();
f、 添加组件(按钮);
ff.addCommand(返回);
//g、 添加组件(主屏幕);
g、 添加组分(tf);
g、 添加组件(添加);
g、 addComponent(rb);
g、 addComponent(rb2);
g、 添加组件(全部);
g、 getStyle().setBgColor(0X99CCFF);
g、 addCommand(返回);
g、 addCommandListener(新ActionListener()
{
已执行的公共无效行动(行动事件ae){
f、 show();
}
}
);
addCommandListener(新的ActionListener()
{
已执行的公共无效行动(行动事件ae){
f、 show();
}
}
);
ff.getStyle().setBgColor(0X99CCFF);
试一试{
recordStore2=RecordStore.openRecordStore(“我的recordStore2”,true);
}
捕获(例外情况除外)
{ }
尝试
{
recordStore=recordStore.openRecordStore(“我的记录存储”,true);
}
捕获(例外情况除外)
{ }
好的,听着
RecordEnumeration enumeration = recordStore.enumerateRecords(null, null, false);
public String [] getRecordData()

    {

    String[] str = null;

    int counter = 0;

    try

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

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

        str = new String[recordStore.getNumRecords()];

        while(enumeration.hasNextElement()){

            try{

                str[counter] = (new String(enumeration.nextRecord()));
                counter ++;

            }catch (Exception e){
                  e.printStackTrace();
            }
        }
     } catch (Exception e){
            e.printStackTrace();
     }
     return str;
}
recordStore2 = RecordStore.openRecordStore("NameOfrecordStore2", true);
RecordEnumeration enumeration = recordStore2.enumerateRecords(null, null, false);
addition.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent ae) {
           String s =tf.getText();
           if(rb.isSelected()){
               if(s!=null && s.length() > 0){
                   try{
                       byte bytestream[] = s.getBytes()  ;
                       recordStore = RecordStore.openRecordStore("RecordStore1", true);
                       int i = recordStore.addRecord(bytestream, 0, bytestream.length);
                   }catch(Exception ex) {   
                       ex.toString();
                   }
               }
            }else if (rb2.isSelected()){
                if(s!=null && s.length() > 0){
                    try{
                        byte bytestream[] = s.getBytes()  ;
                        recordStore2 = RecordStore.openRecordStore("RecordStore2", true);
                        int i = recordStore2.addRecord(bytestream, 0, bytestream.length);
                    }catch(Exception ex) { }
                }

            }
        .....              
       }
   });
  All.addActionListener(new ActionListener() {            
         public void actionPerformed(ActionEvent ae) {
             ff.removeAll();
            try{
                com.sun.lwuit.Label l = new com.sun.lwuit.Label("First Team Goals") ;
                 ff.addComponent(l);
                 String [] record=  getRecordData();
                 List mylist = new List(new DefaultListModel(record));
                 ff.addComponent(mylist);

                 com.sun.lwuit.Label ll = new com.sun.lwuit.Label("Second Team Goals") ;
                 ff.addComponent(ll);
                 String [] record2=  getRecordData2();
                 List mylist2 =new List(new DefaultListModel(record2));
                 ff.addComponent(mylist2);
            }catch(java.lang.IllegalArgumentException e){}
            ff.show();                
         }
    });