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
shamusschubert
Posts: 117
Registered: ‎10-22-2008
My Device: Not Specified
Accepted Solution

Simple ListField example

I found tons on partial code snippets regarding ListField.

 

Can someone post a link to an end-toend sample?

 

Basically how do I add items to a list view and implement selections and format the items, etc.

 

Thanks.

 

Eclispse 3.4
JDE 5
Bold/Tour/Storm
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Simple ListField example

Try the KB.  There are a couple on there.  I think I like one and hate the way the other is done, so have a look and all you find and choose the one you like the look of.  If you have any problems or can't find them, come back, and I'll do a search myself. 

New Developer
zyao
Posts: 1
Registered: ‎01-12-2010
My Device: Curve 8900

Re: Simple ListField example

Hey,

I think I might need a tutorial on ListFields too, but I don't know what KB means ??:smileyhappy:

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Simple ListField example

@zyao - Welcome. 

 

Sorry.  KB - KnowledgeBase, one of the many resources you will find here:

http://na.blackberry.com/eng/developers/resources/

 

I've had a look, and there at least 4 examples, in the KB and I don't like any of them.  This is the least bad.

 

How to - Make list items appear on a screen 
Article Number: DB-00127
http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800345/...

 

Hopefully this is enough to get you going.  If not, you know where to ask.

Developer
shamusschubert
Posts: 117
Registered: ‎10-22-2008
My Device: Not Specified

Re: Simple ListField example

Awesome - I got my basic list to work. 

 

Now on to the next dumb question...

 

How do I listen for click and move to another screen?  I do not see any methods for doing such?

 

Side question, how can I use a Vector of customer business objects (classes) to populate my list, then pass that object to the detail screen when a user clicks on a row.


BTW, here is my simple working code if anyone is interested:

 

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import java.util.*;

 

public class TeamListScreen extends MainScreen
{
 VerticalFieldManager _vfm;
 LabelField lblBoys;
 LabelField lblGirls;
 
 ListField _lf;
 
 TeamListScreen()
 {
  _vfm = new VerticalFieldManager();
  
  lblBoys = new LabelField("Boys Teams");
  lblGirls = new LabelField("Girls Teams");
  
  _lf = new ListField();
  ListCallBack _callback = new ListCallBack();
  _lf.setCallback(_callback);
  
  _lf.insert(0);
  _callback.insert("Testing 123", 0);

  _lf.insert(1);
  _callback.insert("Testing 123", 1);

  _lf.insert(2);
  _callback.insert("Testing 123", 2);

  this.setTitle("Teams");

  _vfm.add(lblBoys);
  _vfm.add(_lf);
  
  this.add(_vfm);
 
 }
 
  protected boolean onSavePrompt()
   {
       return true;
   }
 
  class ListCallBack implements ListFieldCallback
  {
   private Vector listElements = new Vector();
  
   public void drawListRow(ListField list, Graphics g, int index, int y, int w)
   {
    String text = (String)listElements.elementAt(index);
    g.drawText(text, 0, y, 0, w);
   }
  
   public Object get(ListField list, int index)
   { return listElements.elementAt(index); }
  
   public int getPreferredWidth(ListField list)
   { return Display.getWidth();}
  
   public void insert(String toInsert, int index)
   { listElements.insertElementAt(toInsert, index); }
  
   public void erase()
   { listElements.removeAllElements(); }

  public int indexOfList(ListField listField, String prefix, int start) {
   return listElements.indexOf(listField);
  }
  }
}

Eclispse 3.4
JDE 5
Bold/Tour/Storm
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Simple ListField example

Well done!

 

To answer your questions:

 

1) How do I listen for click and move to another screen?

 

Interesting.  Most people will get to override navigationClick on the ListField.  I never do that.  I let the click run the Menu processing as standard, in the menu creation processing (makeMenu override in MainScreen) I see if the ListField is in focus and get the selected item, then I populate the menu based on the attributes of the the selected item.  The user then uses the menu to navigate.  I like this approach better.

 

2) how can I use a Vector of customer business objects (classes) to populate my list,

 

This is why I don't like the supplied samples.  In all my ListFields, I have a completely separate Collection.  I use the Collection count to set the size of the ListField, and I use the ListField get method to get the appropriate item from my collection.  So I don't faff round with the inserts that you have done below.  I'd love you to update your sample to use this approach, and post it, then I can point people at a good example (well what I think is a good example anyway). 

 

3) pass that object to the detail screen when a user clicks on a row

 

This is partially related to (1).  In the menu creation processing, I extract the currently selected item (using get, as noted from (2)).  Then in my actual menu processing (run method) I have a reference to that, that I can pass to the next Screen.  Generally I create a new Screen, so I include the selected item in the COnstructor for the screen, so the menu run method just creates a new Screen and pushes it.

 

Hope this helps.

Developer
shamusschubert
Posts: 117
Registered: ‎10-22-2008
My Device: Not Specified

Re: Simple ListField example

Code samples?

 

1) Collections

 

2) click events on rows

 

Thanks.

Eclispse 3.4
JDE 5
Bold/Tour/Storm
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Simple ListField example

1) Collections

 

No code sample needed here.

 

Let us say I have an applicatins that maintains a list of Service Calls on the BlackBerry.  So in Persistentstore I have a list of calls that this Blackberry user might respond to.  This will probably be a Vector.

 

The user migth want to display all calls.  So I will tell the ListField that it is to display however many there are in my Vector, and I will, in the get(..) method, extract items from the Vector.

 

Now the user wants to display a list of these that are not allocated, so I will filter the Vector into say an Array, and so my ListField will display whatever number are in my Array.  My get(..) method will get the items from the Array. 

 

Here you have two collections, a Vector or an Array.  I could display either, usng the same ListField, by just telling the listFIeld how many it has to display, and making sure the get(..) processing obtained its details from the Vector or the Array.

 

Does that make sense?

 

2) Click on rows

 

As noted, I never do this.  Instead I let people use the Menu button, or let the standard click processing create a Menu, and then I process the selected row. 

 

My makeMenu will look something like this:

 

    protected void makeMenu(Menu menu, int instance) {
       
        int selectedIndex = -1;
        Object currentlySelectedItem = null;
        if ( _listField == null ) {
            selectedIndex = -1;
        } else {
            selectedIndex = _listField.getSelectedIndex();
            currentlySelectedItem = get(_listField, selectedIndex);
        }
        // Can now add menu items that are applicable to this specific item.

 

 

 

Developer
shamusschubert
Posts: 117
Registered: ‎10-22-2008
My Device: Not Specified

Re: Simple ListField example

I know I am drifting from the original topic, but you are very helpful.  If I add a complex type (like a class object) to a Vector, how do I reference them when I add them to the ListField?

 

Say I have a class with a number of properties.  I want the 'Title' property to show as the string in the ListField.

 

    _lf = new ListField();
  ListCallBack _callback = new ListCallBack();
  _lf.setCallback(_callback);
  
  _lf.insert(0);
  _callback.insert("Some team named XYZ", 0);  // refer to class property here instead of "Some team named XYZ" somehow ??

 

 

 

Sorry, for being dense, and thanks for the snippet, but where in the ListField to I attach the menu?  Is makeMenu an override of the default menu?

 

Personally, I think invoking a menu is my second choice.  Since there is only one master/detail action (go to the detail screen), a menu does not seem like the logical UI choice.  I would prefer to select the row and just go straight to the detail screen without again having to click a menu item.

 

Thanks for the help.

 

Eclispse 3.4
JDE 5
Bold/Tour/Storm
Developer
shamusschubert
Posts: 117
Registered: ‎10-22-2008
My Device: Not Specified

Re: Simple ListField example

This worked!

 

  Team t = new Team("Some Team");
  _lf.insert(0);
  _callback.insert(t, 0);

....snipped...

 

   public void drawListRow(ListField list, Graphics g, int index, int y, int w)
   {
    String text = ((Team)listElements.elementAt(index)).getName();
    g.drawText(text, 0, y, 0, w);
   }

 

 

I added the business object (the 'Team' class) to the vector, then called the getName() method to get the string.

 

Now on to figuring out the row click thing.

Eclispse 3.4
JDE 5
Bold/Tour/Storm