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: 367
Registered: ‎11-25-2009
My Device: Not Specified
Accepted Solution

Best Implementation for a callback method to update different fields?

Hey everyone,

 

I am developing an application that makes use of the Google Translate API.  I have it set up so that a user can specify a language before logging in, and then once they log in, I have wrappers around every piece of text displayed to screen which will translate the text based on the language chosen.

 

My problem is that multiple things need to be translated on a given screen.  Therefore, I have to access the Google Translate API multiple times per screen.  To do this, I open an HTTPConnection on a new thread.  So I pass my Screen Interface to the Translator class, and then the Translator class will call my callback methods for that Screen (requestSucceeded() or requestFailed()).

 

Now, lets say I translate both the Screen title and the Screen body text.  That's two separate HTTPConnections.  So when the title is translated, I want my requestSucceeded() method to setText on my title LabelField.  When the body text is translated, I want my requestSucceeded() method to setText on my body LabelField.

 

What is the best way to have my requestSucceeded() method differentiate between different fields to be updated?  Currently, I am passing to the Translator two things (via a String[] array): The text to be translated and a "cookie" describing the item being translated.  I then use if statements in my requestSucceeded() callback method to update the appropriate field based on the "cookie" (which the Translator will send back along with the translated text). 

 

I feel like there has to be a better way to do all of this. Does anyone have any suggestions?

 

Thanks!

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

Re: Best Implementation for a callback method to update different fields?

i think your method is fine.

depending on the size of your screen and the number of fields you may quicken the processing by indexing the fields in an hashtable or 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: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Best Implementation for a callback method to update different fields?

I'm not sure I understood your question correctly, but it looks like it's about decoupling the the background translation task from the UI element that is being translated. You are already half-way at decoupling the background translator from the mechanism that updates the UI. What you appear to be missing is a nice way of specifying the target of the translation without writing lots of switch/if statements.

 

Instead of having one translation callback object that serves multiple UI elements, you could create a separate callback for each UI element. The callbacks will still implement the same interface and the background translator task will thus not care. For example, the interface could be something along the lines of:

 

 

interface TranslationCallback {
  void translationCompleted(String translation);
  void translationFailed(Throwable cause);
}

 

Your UI would then create a callback object that wraps a UI element (e.g., a text field) and implements the above TranslationCallback. Inside the translationCompleted and translationFailed the logic will not need to figure out which element to update, because the callback is associated with a particular element of a particular class, e.g., TextField.

 

Developer
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Best Implementation for a callback method to update different fields?

Great idea.  I changed my implementation to this and it works great. Thanks!

Highlighted
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Best Implementation for a callback method to update different fields?

You're welcome!