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
New Developer
Posts: 15
Registered: ‎12-20-2009
My Device: Not Specified
My Carrier: Sprint

operating on specific objects with run

I've extended ObjectListField for the _serviceList object below; _services is a Vector.

 

Under makeMenu(), I can customize my menu with the currently selected object via

 

String svc = _services.elementAt( _serviceList.getSelectedIndex()).toString();
menu.add( new MenuItem( "Edit " + svc, 115, 11 )
{
public void run()
{
Dialog.alert("Editing " + svc); //throws error :(
}
});

 However, I cannot pass "svc" or do anything with the selected item from my _serviceList inside the run() method (which makes sense based upon the documentation).

 

 

I've tried calling calling a method outside of the makeMenu() method, hoping that the getSelectedIndex() would remain static, but once it the new method is called, getSelectedIndex() appears to reset to the first element of the _services vector.

 

 I hope what I'm trying to accomplish is clear enough that someone can point me in the right direction!

 

Thank you in advance.

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: operating on specific objects with run

[ Edited ]

You want to change the menu text dynamically by overriding makeMenu() in your screen class.

 

Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: operating on specific objects with run

The way that I got around this it to implement  an Interface that extends menu item so that I can pass information to it to use in the run method. Here is how I do it:

 

 

public abstract class CustomMenuItem extends MenuItem {
   public CustomMenuItem(String text, int ordinal, int priority){
      super(text,ordinal,priority);
   }
   public abstract void passData(Object o);
}

 

 

Here is an exampl of how I use it:

 

 

CustomMenuItem close = new CustomMenuItem("Close",60000,1){
   AppScreen as;
   public void passData(Object o){
      as = (AppScreen)o;
   }
   public void run(){
      as.close();
   }
};
close.passData(this);

 

 

Highlighted
New Developer
Posts: 15
Registered: ‎12-20-2009
My Device: Not Specified
My Carrier: Sprint

Re: operating on specific objects with run

[ Edited ]

RexDoug,

 

Thanks for the response.

 

I am overriding  makeMenu() and can successfully display the "svc" variable within the menu. However, I want to use the value of svc (or the value of _serviceList.getSelectedIndex()) within the run() method once the menu item is selected, and that is where I am having the issue.

 

EDIT: corrected name.