java - 单击另一个选项卡的按钮后如何执行包含在一个选项卡中的try-catch-Blackberry

标签 java database sqlite blackberry user-interface

我正在处理下面的代码,我在 Blackberry 的 Pane 管理器中创建了 3 个选项卡。第一个选项卡允许用户根据客户名称选择日期范围,并获取该客户的结果以显示在第三个选项卡中以网格格式。

  1. 所以第一个选项卡有 2 个日期字段和一个客户文本字段。还有一个搜索按钮。

  2. 点击搜索按钮,它会选择搜索记录并跳转到第三个选项卡,以网格格式显示。

  3. 第三个选项卡具有 try catch 语句,用于将表记录元素逐个字符串地插入到网格中。

现在的问题是当我打开我的应用程序并单击第三个选项卡时,它会显示充满垃圾值的网格。它不会等待第一个选项卡的搜索按钮被单击然后显示结果.如果我碰巧关闭应用程序并重新打开它,我会发现第 3 个选项卡显示我之前搜索的结果。

代码如下:

   // setup the tab model with 3 tabs
  final PaneManagerModel model = new PaneManagerModel();
  model.enableLooping( true );

  // setup the first tab   XXXXXXXXXXXXXXXXXXXX~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   VerticalFieldManager vfm = new VerticalFieldManager( 
      Field.USE_ALL_HEIGHT | Field.USE_ALL_WIDTH |
      Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL );
  LabelField lbl = new LabelField( "Content for tab 1", Field.FOCUSABLE );
  vfm.add( lbl );

  TextField1 = new TextField(" Name:            ",null)
     {
            protected boolean keyChar(char ch, int status, int time) 
            {
            if (CharacterUtilities.isLetter(ch) || (ch == Characters.BACKSPACE || (ch == Characters.SPACE))) 
            {
            return super.keyChar(ch, status, time);
            }
           return true;
            }
        };
     vfm.add(TextField1);

    SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");//dd/MM/yyyy
     Date now = new Date();
     String strDate = sdfDate.format(now);

     TextField2 = new TextField("\n From:             ",strDate);
     vfm.add(TextField2);

    // TextField3 = new TextField("TO: ",null);
     //vfm.add(TextField3); 

     SimpleDateFormat sdfDate1 = new SimpleDateFormat("dd/MM/yyyy");//dd/MM/yyyy
     Date later = new Date();
     String strDate1 = sdfDate1.format(later);

     TextField3 = new TextField("\n To:                 ",strDate1);
     vfm.add(TextField3);

     ButtonField showInputButton = new ButtonField("  Search  ",ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
      showInputButton.setChangeListener(new FieldChangeListener() 
      {
            public void fieldChanged(Field field,int context) 
            {
                    Dialog.alert(TextField1.getText());
                    try
                  {    
                    //Open or create the database
                    Database db = DatabaseFactory.openOrCreate("database1.db");
                    //Insert onto table
                    Statement statement13 = db.createStatement("INSERT into 
                    Temp4(date,bill,narration) VALUES (('"+TextField3.getText()+"'),(SELECT balance FROM Temp3),('Opening Balance'))");
                    statement13.prepare();
                    statement13.execute(); 

                    Statement statement131 = db.createStatement("INSERT INTO Temp4(date,bill,narration,id) select date,amount,narration,id from Bills where name=\'"+TextField1.getText()+"\' AND substr(date,7)||substr(date,4,2)||substr(date,1,2) < substr (\'"+TextField3.getText()+"\',7)||substr (\'"+TextField3.getText()+"\',4,2)||substr (\'"+TextField3.getText()+"\',1,2) ");
                    statement131.prepare();
                    statement131.execute();      // date INTEGER,bill INTEGER,rec INTEGER,narration TEXT,id INTEGER
                    statement131.close(); 

                    Statement statement132 = db.createStatement("INSERT INTO  Temp4(date,rec,narration,id) select date,amount,narration,id from Receipts where name=\'"+TextField1.getText()+"\' AND substr(date,7)||substr(date,4,2)||substr(date,1,2) < substr (\'"+TextField3.getText()+"\',7)||substr (\'"+TextField3.getText()+"\',4,2)||substr (\'"+TextField3.getText()+"\',1,2) ");
                    statement132.prepare();
                    statement132.execute();       
                    statement132.close(); 

                     db.close();
                    }
                    catch( Exception e ) 
                    {         
                        System.out.println( e.getMessage() );
                        e.printStackTrace();
                    }
                    model.getView().jumpTo(2,PaneManagerView.DIRECTION_NONE);
                  }
        }

                    );

      vfm.add(showInputButton);
      LabelField myLbl = new MyLabelField( "Ledger" );
 NullField nullFld = new NullField( Field.FOCUSABLE );
  HorizontalFieldManager hfm = new HorizontalFieldManager();
  hfm.add( nullFld );
  hfm.add( myLbl );

  Pane pane = new Pane( hfm, vfm );
  model.addPane( pane );

   //Here ends tab 1 code

//设置第三个选项卡- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~

      vfm = new VerticalFieldManager( 
      Field.USE_ALL_HEIGHT | Field.USE_ALL_WIDTH |
      Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL );

      myLbl = new MyLabelField( "Daily Report" );

      //Adding grid format for fetching from temp4 table----------------


 final GridFieldManager grid = new GridFieldManager(10,5,0); 

    grid.add(new LabelField("Date")
    {
       public void paint(Graphics graphics)
       {
         graphics.setColor(Color.CYAN);
         super.paint(graphics);
       }
     });
    grid.add(new LabelField("Bill")
    {
       public void paint(Graphics graphics)
       {
         graphics.setColor(Color.CYAN);
         super.paint(graphics);
       }
     });
    grid.add(new LabelField("Receipt")
    {
       public void paint(Graphics graphics)
       {
         graphics.setColor(Color.CYAN);
         super.paint(graphics);
       }
     });
    grid.add(new LabelField("Narration")
    {
       public void paint(Graphics graphics)
       {
         graphics.setColor(Color.CYAN);
         super.paint(graphics);
       }
     });
    grid.add(new LabelField("ID")
    {
       public void paint(Graphics graphics)
       {
         graphics.setColor(Color.CYAN);
         super.paint(graphics);
       }
     });

    grid.setColumnPadding(20);
    grid.setRowPadding(20);
//try catch was here


       try
               {

                  //Open or create the database
                    Database db = DatabaseFactory.openOrCreate("database1.db"); 

                    Statement statement55 = db.createStatement("CREATE TABLE IF NOT EXISTS Temp4(date INTEGER,bill INTEGER,rec INTEGER,narration TEXT,id INTEGER)");
                    statement55.prepare();
                    statement55.execute();       
                    statement55.close();

                    Statement statement56 = db.createStatement("SELECT date,bill,rec,narration,id FROM Temp4 ORDER BY ROWID DESC");
                    statement56.prepare();
                    statement56.execute();

                            Cursor c = statement56.getCursor();

                            //Get to the row of grid
                             for (int i = 1; i < grid.getRowCount(); i++)
                             {
                                    System.out.println("Inside for first loops");
                                    //Get to the column of grid
                                for (int j = 0; j < grid.getColumnCount() ; j++)
                                {
                                   System.out.println("Inside for second loops");
                                   //Get to the row of temp4 table
                                   while(c.next()) 
                                   {

                                      System.out.println("Inside while"); 
                                        Row r;
                                        r = c.getRow();
                                        //Get to the column of temp4 table

                                        for (int k = 4; k >=0; k--)
                                        {

                                            System.out.println("Inside for loops");
                                            //Check for whether column retrieved is date or naraation
                                            if(k==0 || k==3)
                                            {
                                                System.out.println("Retrieving date or narration");
                                                grid.insert(new LabelField(r.getString(k))
                                                {
                                                    public void paint(Graphics graphics)
                                                    {
                                                    graphics.setColor(Color.GOLD);
                                                    super.paint(graphics);
                                                    }
                                                 },i,j);


                                            }  
                                            else
                                            {   
                                                //Check for whether column retrieved is bills,rec or id
                                                System.out.println("Retrieving other values"); 
                                                String p = "" + r.getObject(k);

                                                //if(r.getString(k) != null)
                                                //{ 
                                                grid.insert(new LabelField(p)
                                                {
                                                    public void paint(Graphics graphics)
                                                    {
                                                    graphics.setColor(Color.GOLD);
                                                    super.paint(graphics);
                                                    }
                                                 },i,j); 
                                               //  } 


                                            }   
                                           grid.setBackground(BackgroundFactory.createLinearGradientBackground(Color.MIDNIGHTBLUE,Color.STEELBLUE,Color.MIDNIGHTBLUE,Color.STEELBLUE));
                                           //grid.setBackground(BackgroundFactory.createLinearGradientBackground(Color.GOLD,Color.CHOCOLATE,Color.GOLDENROD,Color.CORAL));

                                        } 
                                         System.out.println("Exiting while");                        
                                      }

                                      System.out.println("Exiting sec for");
                                      break;
                                  }
                                System.out.println("Exiting first for");
                                break;
                               } 
                               statement56.close(); 
                               db.close();
                  }

                  catch( Exception e ) 
                  {         
                        System.out.println( e.getMessage() );
                        e.printStackTrace();
                  }  





    vfm.add(grid);

   //----------------grid ends---------------------------------------- 

  nullFld = new NullField( Field.FOCUSABLE );
  hfm = new HorizontalFieldManager();
  hfm.add( nullFld );
  hfm.add( myLbl );

   pane = new Pane( hfm, vfm );
   model.addPane( pane );

现在剩下的设置

      // select the tab to be displayed
  model.setCurrentlySelectedIndex( 0 );    

  // setup the rest of the components
  HorizontalTabTitleView titleView = new HorizontalTabTitleView( Field.FOCUSABLE );
  titleView.setNumberOfDisplayedTabs( 3 );
  titleView.setModel( model );

  PaneView paneView = new PaneView( Field.FOCUSABLE );
  paneView.setModel( model );

  PaneManagerView view = new PaneManagerView( 
          Field.FOCUSABLE  | Manager.NO_VERTICAL_SCROLL | 
          Manager.NO_HORIZONTAL_SCROLL | Manager.USE_ALL_HEIGHT | 
          Manager.USE_ALL_WIDTH, 
          titleView, paneView );
  view.setModel( model );
  model.setView( view );

  // configure the Controller
  HorizontalTabController controller = new HorizontalTabController();
  controller.setModel( model );
  controller.setView( view );
  model.setController( controller );
  view.setController( controller );


  // add the tab manager to the MainScreen
  this.add( view );

 }

我还尝试禁用第三个 Pane 并将其添加到第一个选项卡搜索实现中,但由于第三个选项卡是在第一个选项卡之后定义的,因此出现错误。 我还尝试在函数中包含第三个选项卡的 try catch,以便可以在单击拳头选项卡的按钮时调用它。但这也给出了“非法表达式开始”的错误

请提出一个解决方案。非常感谢任何可以考虑此代码并提供解决方案的人。谢谢。

最佳答案

您是否查看过用于 onExposedonObscured 的 BlackBerry MainScreen 方法?

protected void onExposed()

Invoked when this screen is revealed by a screen getting popped off the display stack. Subclasses of screen should override this method for special handling.

The complimenting callback is onObscured().


protected void onObscured()

Invoked when this screen is obscured by a new screen pushed on the display stack. Subclasses of screen should override this method for special handling.

The complimenting callback is onExposed().

关于java - 单击另一个选项卡的按钮后如何执行包含在一个选项卡中的try-catch-Blackberry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10535909/

相关文章:

Java - 继承的方法com.example.project.ConcreteA无法隐藏com.example.project.MyInterface中的抽象方法

java - 矩形计算器应用程序

java - 服务器向客户端套接字发送消息

java - 如果不在数据库中,Android Studio SQLite 会保存标题吗?

java - java.util.List 实现的测试用例库

java - 比较数据库中的图像

MySQL:什么是页面?

php - 是否可以将 gettext 与来自 mysqli_query 的数据一起使用?

java - 在 Java 中附加 Sqlite 数据库

iphone - SQLite 问题 : sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 不工作