为特定记录存储添加记录时,它还添加到另一个记录存储中!

时间:2013-04-03 13:01:00

标签: java-me lwuit rms

我有一个LWUIT应用程序涉及两个RecordStores players1_recordStore和

players2_recordStore。两种方法方法SearchCarded_team1用于检索

的所有记录

players1_recordStore和方法SearchCarded_team2用于检索

的所有记录

players2_recordStore。 4个按钮b1用于向players1_recordStore添加新记录,b2用于添加新记录

记录到players2_recordStore,b3检索所有记录的player1_recordStore和b4到

检索所有player2_recordStore的记录。

我的申请中有几个问题

1-按b3按钮或b4时,不显示第一条记录!

2-为什么在player1_recordStore的记录旁边显示了players2_recordStore的记录

按b3按钮时反之亦然。

import javax.microedition.rms .*;

 import com.sun.lwuit.plaf.*;
import javax.microedition.midlet.*;

import com.sun.lwuit.*;
import com.sun.lwuit.events.*;

public class HelloLWUITMidlet extends MIDlet    {

     private RecordStore players1_recordStore;
     private RecordStore players2_recordStore;



  public String [] SearchCarded_team1()
{

    String[] str = null;
    int counter = 0;
    try
    {
   RecordEnumeration enumeration = players1_recordStore.enumerateRecords(null, null, false);
        str = new String[players1_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 [] SearchCarded_team2()
{

    String[] str = null;
    int counter = 0;
    try
    {


        RecordEnumeration enumeration = players2_recordStore.enumerateRecords(null, null, false);
        str = new String[players2_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 void startApp()

    {

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

int bgColor =0x0066CC;
try
{
players1_recordStore.openRecordStore("My Record Store", true);

}
catch(Exception e){}

try
{
players2_recordStore.openRecordStore("My Record Store", true);

}

catch(Exception e){}

final form5 gggg =new form5();
gggg.setTitle("Cards ' Record");

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


 final  com.sun.lwuit.Form f = new com.sun.lwuit.Form("Soccer Software");
   f.getStyle().setBgColor(0X99CCFF);

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

 final    com.sun.lwuit.TextField     tf3 =new com.sun.lwuit.TextField();
  final    com.sun.lwuit.TextField  txtfield4 =new com.sun.lwuit.TextField();

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


  final Button cards = new Button(""+"Cards Status");
  cards.getStyle().setBgColor(0X0066CC);

 final Button b1 = new Button("Yellow Cards ");
 final Button b2 = new Button("Red Cards");
 final Button b3 = new Button("All yellow carded players");
 final Button b4 = new Button("All red carded players");

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

  b1.getStyle().setBgColor(0X0066CC); ;
  b2.getStyle().setBgColor(0X0066CC);
  b3.getStyle().setBgColor(0X0066CC); ;
 b4.getStyle().setBgColor(0X0066CC);


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



final com.sun.lwuit.TextArea text = new com.sun.lwuit.TextArea();

final Label lll = new Label("");


 f.addComponent(cards);

 txtfield3.setLabelForComponent(lll);


gggg.getStyle().setBgColor(0X99CCFF);
gggg.addComponent(b1);
gggg.addComponent(b2);
gggg.addComponent(b3);
gggg.addComponent(b4);
gggg.addComponent(txtfield6);


gggg.addCommand(back);


cards.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent ae) {


gggg.show();
     }

  }
);


 gggg.addCommandListener(new ActionListener()

 {


  public void actionPerformed(ActionEvent ae) {



f.show();
     }



 }
          );



b1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

String player1 =txtfield6.getText();

     { }

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

{


try
{
players1_recordStore.openRecordStore("My Record Store", true);
        }

 catch(Exception e){}


 try{
   byte bytestream[] = player1.getBytes()  ;

             int i = players1_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(player1+"-"+"has been carded");
        validDialog.addComponent(textArea);

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

txtfield6.clear();


}


else

{

    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 the record ");
        validDialo.addComponent(textArea);


        validDialo.show(50, 50, 50, 50, true);
        txtfield6.clear();
 }




   }
  }
);

b2.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent ae) {

String player2 =txtfield6.getText();


     { }

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

{

 try{
   byte bytestream[] = player2.getBytes()  ;

             int i = players2_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(player2+"-"+"has been expluded");
        validDialog.addComponent(textArea);

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

txtfield6.clear();


}


else

{

    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 Record");
        validDialo.addComponent(textArea);


        validDialo.show(10, 10, 10, 10, true);
        txtfield6.clear();
 }




   }
  }
);


 b3.addActionListener(new ActionListener() {


  public void actionPerformed(ActionEvent ae) {

  try{
        players1_recordStore = RecordStore.openRecordStore("My Record Store", true);

        }


     catch(Exception ex)
     {
         ex.printStackTrace();
     }

String [] carded =  SearchCarded_team1();
int j;
String valueToBeInserted="";

for( j=0;j< SearchCarded_team1().length;j++)
  {


valueToBeInserted=valueToBeInserted + " " + carded[j];







      }
 Dialog  hilal = new Dialog(" ");
   Style Dialogstyle = hilal.getSelectedStyle() ;
hilal.setTitle("All Yellow Carded Players");
        hilal.setScrollable(false);
 hilal.getDialogStyle().setBgColor(0x0066CC);
        hilal.setTimeout(10000);
        TextArea textArea = new TextArea( );
        textArea.setFocusable(false);

       textArea.setText(valueToBeInserted+"                   "+"\t");
       hilal.addComponent(textArea);

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

  }

  }
);


 b4.addActionListener(new ActionListener() {


  public void actionPerformed(ActionEvent ae) {

  try{
       players2_recordStore = RecordStore.openRecordStore("My Record Store", true);

        }


     catch(Exception ex)
     {
         ex.printStackTrace();
     }

String [] carded2 =   SearchCarded_team2();
int j;
String valueToBeInserted2="";

for( j=0;j< SearchCarded_team2().length;j++)
  {


valueToBeInserted2=valueToBeInserted2 + " " + carded2[j];


      }
 Dialog  hilal2 = new Dialog(" ");
   Style Dialogstyle = hilal2.getSelectedStyle() ;
hilal2.setTitle("All red carded players");
        hilal2.setScrollable(false);
 hilal2.getDialogStyle().setBgColor(0x0066CC);
        hilal2.setTimeout(5000);
        TextArea textArea = new TextArea( );
        textArea.setFocusable(false);

       textArea.setText(valueToBeInserted2+"                   "+"\t");
       hilal2.addComponent(textArea);

        hilal2.show(0, 10, 10, 10, true);
txtfield6.clear();
  }

  }
);

      f.show();

    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void actionPerformed(ActionEvent ae) {
      notifyDestroyed();
    }

public  class form5    extends   com.sun.lwuit.Form


  {



   public form5()

          {

  com.sun.lwuit.Form form5 = new com.sun.lwuit.Form("Cards' Records ");


          }


}





}

1 个答案:

答案 0 :(得分:4)

openRecordStore()方法的第一个参数是RecordStore名称。你打开两次相同的RecordStore; “我的唱片店”。如果你想让它们成为2个不同的唱片店,那么你需要给它们两个不同的名字。