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
Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

How can i implement Click Listener On my Custom ObjectListField ?

Hi all,

 

I have created a Custom ObjectListField extending ObjectListField. I implementged drawListRow method. In my Screen class, i created the object of my CustomObjectListField and displayed the items on the screen.

  How can i implement Click LIstener on this List so that clicking on any row i can do something or call any another screen.

 

   Please help me with some source code example...

 

Thanks and Regards.

Developer
Posts: 16,987
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How can i implement Click Listener On my Custom ObjectListField ?

override the navigation click method
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: How can i implement Click Listener On my Custom ObjectListField ?

First of all, Thanks for your reply.
Could you please tell me where should i override this method? In CustomObjectListField or in Screen(where i created the object of CustomObjectListField ) class?
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How can i implement Click Listener On my Custom ObjectListField ?

CustomObjectListField

Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: How can i implement Click Listener On my Custom ObjectListField ?

Dear peter_strange and simon_hain,

 

   I am posting my code... Please guide me where should i override navigationClick method to do something depending on the row number clicked on...

 

   Sample code please.....

 

Thanks and

public class CustomObjectListField extends ObjectListField{
    
    private static String[] cinemaName;
    private static String[] detailsStr;
    private Bitmap arrowBitmap = Bitmap.getBitmapResource("arrow.png");
        
    public CustomObjectListField( String[] cinemaName ){
    	this.cinemaName = cinemaName;
    }
 
     public void drawListRow(ListField listField, Graphics graphics,int index, int y, int width) {
    	     	 
    	 if (null != arrowBitmap) {
             int offsetYText = (this.getRowHeight() - getFont().getHeight())/2;
             int offsetYBitmap = (this.getRowHeight() - arrowBitmap.getHeight())/2;
             
             graphics.setColor(Color.ORANGE);
             graphics.drawLine(0, y, width, y);
             graphics.setColor(0xFF0000);
			 Background background = BackgroundFactory.createSolidBackground(Color.WHITESMOKE);
             setBackground(background);
             graphics.drawText(cinemaName[index], 15, y+offsetYText, DrawStyle.ELLIPSIS, width-arrowBitmap.getWidth()-5);
             graphics.drawBitmap(width-25, y+offsetYBitmap, arrowBitmap.getWidth(), arrowBitmap.getHeight(), arrowBitmap, 0, 0);
             
          } else{
             graphics.setColor(0xFF0000);             
             graphics.drawText(cinemaName[index], 10, y, (DrawStyle.LEFT|DrawStyle.ELLIPSIS|DrawStyle.TOP), width);              
             int newY = y + (this.getFont()).getHeight() + 1;  // y start for text
             graphics.setColor(0x00FF00);
             graphics.drawText(detailsStr[index],20, newY, (DrawStyle.LEFT|DrawStyle.ELLIPSIS|DrawStyle.TOP), width);
          }
     }
     
};

 

 

public class CinemaHallScreen extends MainScreen {
	
	VerticalFieldManager cinemaHallScreenVerticalFieldManager = null;
	VerticalFieldManager verticalFieldManager = null;
	private Vector allCinemasInfoVector = null;
	private String[] cinemaName = null;
	
	public CinemaHallScreen() {
				
		PleaseWaitPopupScreen.showScreenAndWait(new Runnable() {

			public void run() {

				allCinemasInfoVector = WaveServices.getAllCinemas();
				if(allCinemasInfoVector == null){
					WaveCinemaUtils.displayAlert("Server Connection not available.");
				}else if(allCinemasInfoVector.size() == 0){
					WaveCinemaUtils.displayAlert("No Cinemas found.");
				}else{
					updateObjectListField();
				}
			}
		}, "Please wait...");
			
	}
		
	public void updateObjectListField()
	{
		UiApplication.getUiApplication().invokeAndWait(new Runnable()
		{
			public void run()
			{
				synchronized(Application.getEventLock())
				{
					verticalFieldManager = new VerticalFieldManager();
					cinemaName = new String[allCinemasInfoVector.size()];
					for(int i=0; i<allCinemasInfoVector.size();i++){
						cinemaName[i] = ((AllCinemasInfo)allCinemasInfoVector.elementAt(i)).getCinemaName();
					}
					CustomObjectListField customObjectListField = new CustomObjectListField(cinemaName);
					customObjectListField.setRowHeight(2 * (customObjectListField.getFont().getHeight()) + 2);
					customObjectListField.set(cinemaName);
									
					verticalFieldManager.add(customObjectListField);
		           	add(verticalFieldManager);
									
				}
			}
		});
	}
}

 

 

 

 

 

Regards.

Developer
Posts: 16,987
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How can i implement Click Listener On my Custom ObjectListField ?

in your customlistfield.

and as a sidenote: in most cases invokeAndWait is not a good thing to use, and you don't have a usecase for it anyhow. replace it with invokeLater. you also don't need the sync on the eventlock, remove it.

On the other hand, your call to the getAllCinemas method looks like a blocking network call. you should execute that in a separate thread. (maybe you do that with the runnable, just want to be sure here)
see http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865 for details.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How can i implement Click Listener On my Custom ObjectListField ?

I agree with all the points that Simon as raised.

 

And just to expand a little on the answer to this question:

"Please guide me where should i override navigationClick method to do something depending on the row number clicked on..."

 

The best way, for me anyway, is to override navigationClick, and initiate a fieldChange.  The you can listen to a FieldChange in you Screen object, in the same way as it processes a row.  You can find out the selected row directly from the ListField, see getSelectedIndex(). 

 

There is some sample code in here:

http://supportforums.blackberry.com/t5/Java-Development/Implementing-Listener-for-Custom-ListField/m...

 

Note that you do not have to override touchEvent - if you don't it percolates to navigationClick anyway. 

Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: How can i implement Click Listener On my Custom ObjectListField ?

Sir, Actually I am new to blackberry application development...i used this invokeAndWait to wait until i get some response data from webservice call... According to my understanding, invokeLater would have resulted into execution of next statement without waiting for the response data from webservice call....

Any suggestion for this from your side Sir....?
Developer
Posts: 16,987
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How can i implement Click Listener On my Custom ObjectListField ?

your assumption is correct. blocking your ui is not. if the ui thread is unresponsive for too long the system will kill it (and your app).

that is the reason for using a separate thread for networking (or other blocking operations). the thread triggers an ui update when it is finished.

if you want to display something to the user while the thread is working you can look for peters "please wait" samples or something similar.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: How can i implement Click Listener On my Custom ObjectListField ?

Sir, I used the runnable to call webservice method as you can see in my code like

(new Runnable() {

public void run() {

allCinemasInfoVector = WaveServices.getAllCinemas();
.............................................
....................

Is it right approach?

Regards,