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

Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.

Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Simple ListField example

[ Edited ]

Here is a sample that demonstrates most of the things I've been trying to say, including the way of having a collection, how to decide which one has been selected, and having "intelligent" menu items.


Of course there are parts of this that most people would frown on, most notably the way the UI actually understands the data and so the business logic.  If you were doing this properly, you would create a class that displayed People, and it would provide a ContextMenu to the MainScreen depending on whether an item in its ListField was selected.


Please everyone review and suggest improvements, I would like to create a better example if I can, but not if this means sacrificing readability and understandability for newer BB developers.


class TeamListScreen extends MainScreen
                            implements ListFieldCallback {

    // Two listFields, one to display the Girls, the other to display the Boys
    private ListField _lfBoys;
    private ListField _lfGirls;

    private Person _selectedPerson = null; // Selected Person
    private boolean _selectedPersonIsABoy = false;

    // We display Data from these Vector
    private Vector _listBoys = new Vector(5);
    private Vector _listGirls = new Vector(5);
    // In this case we populate the Vectors with
    // our own data, normally of course this
    // data would be supplied to us

    private MenuItem _displayPersonMenu = new MenuItem("Display", 110, 10) {
        public void run() {
            if ( _selectedPerson != null ) {
                String name = _selectedPerson._firstName + " " +
                              _selectedPerson._initial + " " +
                if ( _selectedPersonIsABoy ) {
                    name = name + " is a Boy";
                } else {
                    name = name + " is a Girl";

    TeamListScreen() {
        // Set up Dummy Data
        _listBoys.addElement(new Person("Joe", "R", "Bloggs"));
        _listBoys.addElement(new Person("Fred", "F", "Flintstone"));
        _listBoys.addElement(new Person("Andy", "IS", "Boring"));
        _listBoys.addElement(new Person("Peter", "IS", "Clever"));

        _listGirls.addElement(new Person("Joesphine", "R", "Bloggs"));
        _listGirls.addElement(new Person("Fredrica", "F", "Flintstone"));
        _listGirls.addElement(new Person("Paticia", "IS", "Clever"));

        this.setTitle("Team Players");

        this.add(new LabelField("Boys"));
        _lfBoys = new ListField(_listBoys.size());
        _lfBoys.setCallback(this); // We manage the interaction!

        this.add(new LabelField("Girls"));
        _lfGirls = new ListField(_listGirls.size());
        _lfGirls.setCallback(this); // We manage the interaction!

    protected boolean onSavePrompt() {
        return true;

    protected void makeMenu( Menu menu, int instance ) {
        // Which List item is selcted?
        _selectedPerson = null; // Not sure which is focused atm.
        Field focusingOn = this.getLeafFieldWithFocus();
        if ( focusingOn == _lfBoys ) {
            _selectedPersonIsABoy = true;
            _selectedPerson = (Person) this.get(_lfBoys, _lfBoys.getSelectedIndex());
        if ( focusingOn == _lfGirls ) {
            _selectedPersonIsABoy = false;
            _selectedPerson = (Person) this.get(_lfGirls, _lfGirls.getSelectedIndex());

    // Implemented Call Back Methods follow

    // draw the curent row
    public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
        Person personToDraw = (Person) this.get(list, index);
        int drawColor = Color.BLACK;
        if ( list == _lfBoys ) {
            // We have a boy!
            drawColor = Color.BLUE;
        } else {
            drawColor = Color.RED;
        g.drawText(personToDraw._firstName + " " + personToDraw._lastName, 0, y, 0, w);
    // get the selected index from the correct Vector
    public Object get(ListField list, int index) {
        if ( list == _lfBoys ) {
            return _listBoys.elementAt(index);
        } else {
            return _listGirls.elementAt(index);
    public int getPreferredWidth(ListField list) {
        return Display.getWidth();

    public int indexOfList(ListField listField, String prefix, int start) {
        // Not a correct implementation - this is really just commented out
        return start;

    // This is the class which describes the data we will be displaying
    class Person {
        // In this example, to save accessors, the names are public
        public String _firstName = null;
        public String _lastName = null;
        public String _initial = null;
        public Person(String firstName, String initial, String lastName) {
            _firstName = firstName;
            _initial = initial;
            _lastName = lastName;



Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Simple ListField example

I've just been pointed at this sample, which I think is very good.



Posts: 17
Registered: ‎12-01-2010
My Device: Not Specified

Re: Simple ListField example

that was one neat post .... did my entire list and database access based on ur post ... !!!!

Posts: 16
Registered: ‎05-03-2011
My Device: 98000
My Carrier: Student

Re: Simple ListField example


First of all I want to say that I m beginner in Java BlackBerry.

I can show a result of my JSON file by RichTextField()method without any problem. Now I want to put my JSON file in ListField so that show the result in the shape of list. In the first time I implemented ListFieldCallback interface. In the second time I have created class (back) where I added my list and associated it to the implementation of ListFieldCallback.




public class Back extends MainScreen {

	 public String NAME;
	 public int index;
	public Back() throws IOException

		LabelField title = new LabelField("Information",
				LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
		String jsonResult = Selection.cxn();
		if (jsonResult != null) {
			ListField list = new ListField();
			ListCallback callback = new ListCallback();
			try {
				JSONObject json = new JSONObject(jsonResult);
				JSONArray resultlist = json.getJSONArray("table");   
				for (int i = 0; i < resultlist.length(); ++i) {
					 JSONObject element = resultlist.getJSONObject(i);
					 JSONObject Winner = element.getJSONObject("Winner");
					 String NAME = OWNER.getString("NAME");
					 callback.insert(NAME, index);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
		} else {
			add(new RichTextField("Don't found"));




class ListCallback implements ListFieldCallback {
   String nameshow=Showliste.NAME;
   private Vector listElements = new Vector();
   public void drawListRow(ListField list, Graphics g, int index, int y, int w) {

      g.drawText("testing", 0, y, 0, w);
      g.drawText(String.valueOf(nameshow), 0, y, DrawStyle.RIGHT, w);
   public Object get(ListField list, int index) {
       return listElements.elementAt(index);
   public int indexOfList(ListField list, String p, int s) {
       //return listElements.getSelectedIndex();
       return listElements.indexOf(p, s);
   public int getPreferredWidth(ListField list) {
      return Graphics.getScreenWidth();
   public void insert(String NAME, int index) {
       listElements.insertElementAt(NAME, index);
   public void erase() {



When I run my code I recuperate of all NAMEs null like value

help please