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
Regular Contributor
zumwaltwood
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530
My Carrier: Cricket
Accepted Solution

List option confusion.. need recommendation

Looking through the Docs, I have 4 separate options for what I am trying to do, but none of them at the moment pop out at me as having a key value pair option.

 

I have a list returned to me with 2 columns from my webservice of between 1 item and 5 items, so when the user selects the search button, the query goes back to the web service and then the webservice spits back results, column 1 is supposed to be used as a key, column 2 is the value to display, but I do not want to display the key, however the key is very crucial to the rest of the process.

 

I first looked at text-based drop down list, this gives a nice visual look but loss of key, then I looked at the simple list, not as user appealing as the drop down list, graphically anyway, again, loss of key, next I looked at the rich list, cooler looking, again, useless though because I can't hide the key and I do not need all the extra options like a picture or description, just want to show a single string no wider than like 25 characters, so finally I glimpsed at a table, which is way overkill in how much work needs to go into building it, is too fat for my purposes, and again, loss of the ability to have the key and hide it.

 

I simply must be missing something here in what I am reading or their is something not documented, all I need is to populate the list with the 5 items I have, each item have its key associated with it, look at the selected item from the list when the user goes to save the form, pull the key from a key field so I can utilize the key instead of the value because the value can change from time to time but the key is always the same from the server no matter what they do to the original text of the value on the server.

 

Any recommendations?

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Please use plain text.
Developer
jprofitt
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: List option confusion.. need recommendation

I would find whichever one you like the best and extend it to make your own class. From there you can pass it a Vector or Hashtable or whatever you're using to store the data and then override the appropriate methods to get it to pull text from this object. I'm not certain on this, but you may have to override paint as well.

Please use plain text.
Regular Contributor
zumwaltwood
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530
My Carrier: Cricket

Re: List option confusion.. need recommendation

Still new to Java, coming from the .Net world LOL

So basically, what you are suggesting is that I do something like:

import java.util.Vector;
import net.rim.device.api.ui.component.ObjectChoiceField;

public class ListExtended extends ObjectChoiceField {
	Vector<ClientInfo> cInfo=new Vector<ClientInfo>();
	
	public ClientInfo getChoice(int index)
	{
		return cInfo.elementAt(index);
	}
	
	public void setChoices(Vector<ClientInfo> items)
	{
		cInfo.clear();
		cInfo.addAll(items);
	}
}

 

And all my ClientInfo class in it would be the public entities that I want to pass back and forth..

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: List option confusion.. need recommendation

[ Edited ]

Along similar lines....

 

ObjectChoiceField displays Objects using the Object's toString method.  Most people just give ObjectChoiceField an array of Strings, which makes the use of toString() redundant.  However you can give it an array of any objects, as long as the Objects have a toString method (which of course they always do...).  Anyway, point here is that you just create an Object like this

 

class KeyValuePair extends Object {

String _key = null;

String _value = null;

public KeyValuePair(String key, String value) {

_key = key;

_value = value;

}

public String getValue() {

return _value;

}

public String getKey() {

return _key;

}

public toString() {

return _value;

}

}

 

Now create an array of these objects on return from your WebService, and just feed these in as choices to your ObjectChoiceField.  Then you can get the chosen key as follows:

 

int selectedIndex <myObejctChoiceField>.getSelectedIndex();

KeyValuePair selectedKeyValuepair = <myArrayOfKeyValuepairs>[selectedIndex];

String selectedKey = selectedKeyValuepair.getKey();

 

Make sense?

Please use plain text.
Regular Contributor
zumwaltwood
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530
My Carrier: Cricket

Re: List option confusion.. need recommendation

[ Edited ]

Makes great sense, my last attempt at this was to extend the constructors, which also didn't pan out very well, going to give your recommendation a try now, many thanks, will let you know how it goes.

 

 

Brilliant, works like a charm based on your suggestion, now back to getting the selection change on the list to update based on the search dynamically and clear up the screen, a little tricker with the simulator :smileyhappy: This works great though, thanks!

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Please use plain text.