GWT Array of checklist click handler not working

255 Views Asked by At

i have been trying to generate an array of check boxes and dynamic click handler for them but the handler is not working.

Any suggestion will be most welcomed. Thanks in Advance for the time.

private void addButtonListener() {      
    goButton.addClickHandler(new ClickHandler() {           
        @SuppressWarnings("rawtypes")
        @Override
        public void onClick(ClickEvent arg0) {              
            String strQuery="Select BRANCH_NAME from SAMPLE_ACC_BRANCH where GL_CODE='"+gll_textfield.getText().trim()+"'";
            HibernateImplUtils.getSearchResult(strQuery, new AsyncCallback() {                  
                private int i;
                @Override
                public void onFailure(Throwable arg0) 
                {arg0.printStackTrace();}
                @Override
                public void onSuccess(Object arg0) {                        
                    System.err.println("Inside Success");
                    List branchNameList=(List) arg0;
                    System.err.println("Branch List:::"+branchNameList);                        
                        for(i=0;i<branchNameList.size();i++){
                            checkbox[i]=new CheckBox((String) branchNameList.get(i));                               
                            vpanel.add(checkbox[i]);
                            checkbox[i].addClickHandler(new ClickHandler() {                                    
                                @Override
                                public void onClick(ClickEvent arg0) {                                      
                                    if(checkbox[i].getValue()){
                                        System.out.println("NAME::::"+checkbox[i].getText());
                                    }
                                    System.out.println("Selected check box ::::"+checkbox[i].getText());
                                }
                            });                                             
                    }                       

                }
            });             

        }
    });

}   
2

There are 2 best solutions below

3
On BEST ANSWER

The scope of "i" is dodgy. Quickest fix would be to make a "final" copy for your event handler. e.g. "final int i2 = i"

The inner class probably wants the index value during its creation.

I'd be tempted to do use a final reference to the checkbox you create or the reference passed to the event handler (that way you could also use a single instance).

(Modified)

0
On
    final int i2=i;                         
    checkbox[i2].addClickHandler(new ClickHandler() {                                    
      @Override
        public void onClick(ClickEvent arg0) {
        if(checkbox[i2].getValue()){                                           
           System.out.println("NAME::::"+checkbox[i2].getText());
                }

              }
          });