When adding a record for a specific recordstore , it's added to another recordstore also!?

StackOverflow https://stackoverflow.com/questions/15788073

  •  31-03-2022
  •  | 
  •  

Question

I have a LWUIT application that involves two RecordStores players1_recordStore and

players2_recordStore . Two methods method SearchCarded_team1 for retriving all records of

players1_recordStore, and method SearchCarded_team2 for retriving all records of

players2_recordStore . 4 Buttons b1 for adding new record to players1_recordStore,b2 for adding new

record to players2_recordStore , b3 to retrieve all records of players1_recordStore and b4 to

retrieve all records of players2_recordStore.

I have a couple of problem in my application

1- When b3 Button or b4 pressed the first record is not displayed!

2- Why the records of players2_recordStore are displayed beside records of players1_recordStore

when pressing b3 Button and the opposite is true.

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


          }


}





}
Was it helpful?

Solution

The openRecordStore() method's first paramenter is the RecordStore name. You are opening the same RecordStore twice; "My Record Store". If you want them to be 2 different record stores, then you need to give them two different names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top