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
fritzd
Posts: 68
Registered: ‎04-18-2009
My Device: Not Specified

Custom Event Handler/ListFieldEvent Handling Problem

Hey all,

 

I have a custom control called FileListField that inherits from ListField and represents a list of files in a directory of the file system.  When you click on an item in the list and it is a directory, it goes to the new directory and lists the files in the new directory.  However, when you click on a file, I want to raise an event that the encapsulating screen handles called something like FileSelected.  Is this possible within the Blackberry Framework?

 

I have tried things like setting the instance of the list's FieldChangeListener to be handled in the screen, but the problem is that it gets called multiple times when a directory is selected and I don't want to have it fire at all in this case since a file wasn't selected.  I really want to have more control so that when the event fires I can be sure that a file was selected.  Can you create custom listeners and events?  Is there some other event that I should plug in to?

Thank you in advance!  This community is great!

Please use plain text.
Developer
pfiffi78
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: Custom Event Handler/ListFieldEvent Handling Problem

In your ListField, just overwrite the trackwheelClick(...) method and use getSelectedIndex() to lookup what element has been clicked on. Then, if the data element is a file, fire a custom event to whereever you like.
Please use plain text.
New Developer
fritzd
Posts: 68
Registered: ‎04-18-2009
My Device: Not Specified

Re: Custom Event Handler/ListFieldEvent Handling Problem

How do I make a custom event?  The blackberry API seemed limited.  Can you point me to sample code? Thanks.
Please use plain text.
Developer
pfiffi78
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: Custom Event Handler/ListFieldEvent Handling Problem

What special support do you expect from the API? 

 

If you have worked with Java before and know anything about the Observer/Observable design pattern, or more generic Listeners, then you can easily implement all yourself. There's no need for special API support.

 

Just pass your screen as the owner to the ListField via your constructor. That way the ListField has a reference to the screen. Then, whenever a user clicks on a file entry, you call whatever method you need on the screen from within the trackwheelClick() method after you determined that the selected entry in the list is a of type file.

 

 

Please use plain text.
New Developer
fritzd
Posts: 68
Registered: ‎04-18-2009
My Device: Not Specified

Re: Custom Event Handler/ListFieldEvent Handling Problem

I don't expect "special support".    I stated that the package was limited.  For example in the mobile version of java you can't use ArrayList.  I know I can implement it myself, but I was just looking for a little support code.  Observer/Observable got cut from this version of java too! 

 

I don't want to pass in a hard reference to a screen in my generic control.  I guess I will just have to make an interface and construct my own addListener in my observable class.

Please use plain text.
Developer
pfiffi78
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: Custom Event Handler/ListFieldEvent Handling Problem

Instead of ArrayList you can use Vector, the grandfather of all List types in the collection framework introduced in Java 1.2

 

If you have ever used the Oberserver/Oberservable classes in Java, then you will know that they are limited in their use and it's easier to come up with a own implementation based on what you basically just said yourself: A generic listener interface and a add/removeListener() method.

 

All so easy... :-) 

Please use plain text.