2

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

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

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

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

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

这是代码:

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

 }

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

请提出一个解决方案。任何可以对此代码有所思考并帮助解决方案的人将不胜感激。谢谢。

4

1 回答 1

0

您看过 BlackBerry MainScreen方法onExposedonObscured吗?

受保护的无效 onExposed()

当从显示堆栈中弹出的屏幕显示此屏幕时调用。screen 的子类应覆盖此方法以进行特殊处理。

恭维回调是onObcured()


受保护的无效 onObcured()

当此屏幕被显示堆栈上推送的新屏幕遮挡时调用。screen 的子类应覆盖此方法以进行特殊处理。

恭维回调是onExposed()

于 2013-01-10T15:13:43.307 回答