Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
nader22
Posts: 19
Registered: ‎06-30-2011
My Device: 9800
Accepted Solution

Probleme with setChangeListener on a button

Hello everyone,

i have a problem on how to create the listnerfor array of button these buttons take there names from an xml file
there is some lines of my code please i need help

 

  for (temp = 0; temp < nList.getLength(); temp++) {
       
        HorizontalFieldManager hfm = new HorizontalFieldManager();

        nb = nList.getLength();
        sendButton = new ButtonField[nList.getLength()];
       
        Node nNode = nList.item(temp);       
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 
           Element eElement = (Element) nNode;
          
          
        
          
          
          sendButton[temp] = new ButtonField(getTagValue("firstname",eElement),Field.FIELD_RIGHT) {
                protected boolean navigationClick(int status, int time) {
                    fieldChangeNotify(0);
              
                    return true;
                }

                protected boolean keyChar(char character, int status,
                        int time) {
                    if (character == Keypad.KEY_ENTER) {
                        fieldChangeNotify(0);
                        return true;
                    }
                    return super.keyChar(character, status, time);
                }
            };
           Status.show("num"+temp);
         
           i=temp;
           FieldChangeListener listener = new FieldChangeListener() {
               public void fieldChanged(Field field, int context) {
                  context = field.getIndex();
            

                Status.show("label"+context);


                 

               }
           };
           sendButton[i].setChangeListener(listener);
            
         
          
          
           hfm.add(sendButton[temp]);

          mainVerticalManager.add(hfm);

.......

}

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem with setChangeListener on a button

Hi and Welcome to the forums.

 

I'm not sure I understand the problem here.

 

Can you not just create an array that holds all your sendButtons, then create a Listener that knows about this array and add this Listener to all your sendButtons by going through the array?

Contributor
nader22
Posts: 19
Registered: ‎06-30-2011
My Device: 9800

Re: Problem with setChangeListener on a button

hi

well the problem is that i can't create a listner that khnows about this array so please if you can help me with some code it's gonna be perfect

thanks

Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: Problem with setChangeListener on a button

Why do you need the listener which knows about this array? (Not to mention you can actually create one) fieldChanged gets the field that was changed as one of the parameters. If you need the index of the field in your array, setCookie with that index on each field and getCookie inside fieldChanged (that's one of the approaches).

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem with setChangeListener on a button

I like akadyz's apporach. 

 

Here is some reworked code that might help if you wanted to understand my suggestion better:

 

  ButtonField [] sendButton = new ButtonField [nList.getLength()];
    FieldChangeListener listener = new FieldChangeListener() {


        public void fieldChanged(Field field, int context) {
            if ( field instanceof ButtonField ) {
                int matchedIndex = -1;
                for ( int i = 0; i < sendButton.length; i++ ) {
                    if ( field == sendButton[i] ) {
                        matchedIndex = i;
                        break;
                    }
                }
            }
        }
    };


  for (temp = 0; temp < nList.getLength(); temp++) {
      
        HorizontalFieldManager hfm = new HorizontalFieldManager();

        nb = nList.getLength();
      
        Node nNode = nList.item(temp);      
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 
           Element eElement = (Element) nNode;
         
         
       
         
         
          sendButton[temp] = new ButtonField(getTagValue("firstname",eElement),Field.FIELD_RIGHT) {
                protected boolean navigationClick(int status, int time) {
                    fieldChangeNotify(0);
             
                    return true;
                }

                protected boolean keyChar(char character, int status,
                        int time) {
                    if (character == Keypad.KEY_ENTER) {
                        fieldChangeNotify(0);
                        return true;
                    }
                    return super.keyChar(character, status, time);
                }
            };

           sendButton[temp].setChangeListener(listener);

           hfm.add(sendButton[temp]);

        }

        mainVerticalManager.add(hfm);
    }

Contributor
nader22
Posts: 19
Registered: ‎06-30-2011
My Device: 9800

Re: Problem with setChangeListener on a button

i'm really sorry new friends cause i took so long to reply

it does work now thank you very much

problem solved

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem with setChangeListener on a button

Glad to hear you have got this working.

 

Could you please mark this Thread as solved? 

 

If you don't know how, please try the Help Link near the top of the screen.