Lists
We cannot ignore them — they are everywhere.
Lists of things “todo”. Things to buy. Choices to choose from.
No mobile environment would be complete without the ability to display a list of choices to a user.
Of course, every platform has their own name for them. Android calls its list a ListView and requires an “adapter” to supply the data to the list widget.
iPhone developers create lists with the UITableView and flesh out a “protocol” in Objective-C to provide the data via a callback paradigm. For the Java programmers among us, a protocol is similar to an Interface in Java.
In an earlier article we even looked at how to work with lists on the WebOS platform — back when we had high hopes for the OS.
With the introduction of the BlackBerry Torch and some new energy in the BlackBerry platform, I thought it would be worthwhile to look at programming the ListField for the BlackBerry OS.
Here is a road map of this and the next two articles in this series where we look at three progressive examples of working with the ListField:
- Providing data via the ListFieldCallback interface (this article)
- Subclassing and adding color to the list so it doesn’t look so bland
- Displaying data from a persistent data source
Using the ListField
There are two files in the sample project:
- BBColorList.java — this is the entry point of the application, which extends UiApplication.
- BBColorScreen.java — this file contains the class BBColorScreen, which extends MainScreen. This is the primary UI for the sample application.
Here is the code for the BBColorList class.
/*
* BBColorList.java
*
*/
package com.msi.linuxmag.cl;
import net.rim.device.api.ui.*;
class BBColorList extends UiApplication {
BBColorList() {
pushScreen(new BBColorScreen());
}
public static void main(String [] args) {
BBColorList app = new BBColorList();
app.enterEventDispatcher();
}
}
This code is boiler-plate for a BlackBerry app — the entry point of the application is the main() function which creates an instance of the BBColorList app and then enters the main event handling loop. The BBColorList class creates an instance of the BBColorScreen class and “pushes” it to the foreground with a call to pushScreen. Not very exciting, but necessary. Let’s move on to the BBColorScreen class.
/*
* BBColorScreen.java
*
*/
package com.msi.linuxmag.cl;
import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
class BBColorScreen extends MainScreen implements ListFieldCallback{
ListField sampleList;
String [] listData = {"ALICEBLUE","ANTIQUEWHITE","AQUA","AQUAMARINE", \
"AZURE","BEIGE","BISQUE","BLACK","BLANCHEDIAMOND"};
BBColorScreen() {
super(DEFAULT_MENU);
sampleList = new ListField();
sampleList.setSize(listData.length);
sampleList.setCallback(this);
add(sampleList);
setTitle(new LabelField("linuxdls ListField",LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER));
}
/* ListFieldCallback Interface methods */
public void drawListRow(ListField listField,Graphics graphics,int index,int y,int width)
{
graphics.setFont(Font.getDefault());
graphics.drawText(listData[index],2,y,DrawStyle.TOP,width);
}
public int getPreferredWidth(ListField listField)
{
return Display.getWidth();
}
public Object get(ListField listField,int index)
{
return listData[index];
}
public int indexOfList(ListField listField,String prefix,int start)
{
try {
int ret = -1;
for (ret = start;ret<listField.getSize();ret++) {
if (listData[ret].toUpperCase().startsWith(prefix.toUpperCase())) return ret;
}
return 0;
} catch (Exception e) {
return 0;
}
}
/* End of ListFieldCallback Interface methods */
}

Managing data with the ListField
The first thing to note about this class is that it extends the MainScreen class and implements the ListFieldCallback interface. The ListFieldCallback is responsible for supplying data to an associated ListField instance.
Let’s take a moment to discuss the relationship between a ListField and its data. The ListField is responsible for managing a “list” of entries, though it knows very little about the data itself. In fact, the only thing it is aware of is the number of entries it contains. In the sample code, the number of entries in the list is assigned through the setSize() method of the ListField. Alternatively, the size may be passed in to the constructor of the ListField.
When the ListField displays data at runtime, it invokes a method named drawListRow() from its associated callback. The drawListRow is one of four methods required by the ListFieldCallback interface:
- drawListRow — used to draw the text/image onto the list within the specified coordinates.
- getPreferredWidth — used to tell the list how wide it should be drawn. This is typically the full width of the screen.
- get — used to retrieve an entry from the list. Note that its return type is Object.
- indexOfList — this is used to help find an entry from the list.
Jumping back into the code, let’s examine the constructor: BBColorScreen().
This method invokes the base class’ constructor by calling super() and then proceeds to wire up the ListField. This declarative approach to user interface design is typical of BlackBerry applications — you either like it or you don’t. I tend to prefer this over WYSIWYG because I’m a bit of a control freak and like to be able to see everything plainly in code rather than having to worry if I refreshed a separate resource file. At times it can be tedious to define everything “by hand”, I confess.
Once the ListField is created, we tell the list how large it is with a call to setSize(), passing in the number of entries in our array. If we were working with a database (called a RecordStore in BlackBerry-speak), we would likely pass in the number of records available in a RecordEnumeration, which is similar to a “record set”.
Next we assign the callback to be “this” class itself.
We add this ListField to the screen with a call to the add() method and then we assign a LabelField to be the title of the screen. The reason we use a LabelField and not just simple String literal is because we can take advantage of the drawing styles to center the text on the screen. If we had just called setTitle(“some title”), which is a valid approach, the title would be aligned to the left. Just a personal taste issue.
Ok, now we’re ready to look at the methods which are implemented for the ListFieldCallback interface. The first thing to observe about these four methods is that they each pass in an instance of a ListField. This allows the same class to provide data for multiple instances of a ListField at runtime should you choose to organize your code in this fashion. Our code has only a single ListField so it really doesn’t matter to us in this example.
The drawListRow() method takes the most arguments: a ListField instance,a Graphics object, an integer representing the ordinal of the desired list element, a vertical offset and the width available to draw with. This information tells us which object to draw and what kind of boundary we are constrained to stay within.
public void drawListRow(ListField listField,Graphics graphics,int index,int y,int width)
{
graphics.setFont(Font.getDefault());
graphics.drawText(listData[index],2,y,DrawStyle.TOP,width);
}
In this method we assign the selected font with a call to setFont() and then draw the string via the drawText method. Pretty simple. The follow-on articles spend some more time in this method customizing the drawing activity.
Let’s jump to the getPreferredWidth() method. This method passes in a ListField instance and expects an integer return value. Because we want our ListField to span the entire width of the screen, we return Display.getWidth(). The Display class is part of the net.rim.device.api.system package and is a protected API. This means that the application must be signed before running on a physical device.
public int getPreferredWidth(ListField listField)
{
return Display.getWidth();
}
The get() method takes a ListField and integer parameter. The data behind our ListField is just an array of String objects so we can simply return the String associated with the index requested.
public Object get(ListField listField,int index)
{
return listData[index];
}
The indexOfList method takes three parameters: a ListField, a String to search for and a starting index. Implementing this function provides the functionality of searching a ListField by spelling out the desired entry.

Progressive Search Feature: searching for ‘bla
For example, if you have a long list and want to jump down to the entries that start with the letters “BLA”, you will jump to the first list entry which starts with those letters. The user must type fairly quickly to get the progressive search. The key to the progressive search is the start parameter. If you wait too long between keypresses, the search starts again from the top of the list.
The implementation of this code starts by indexing into the listData array at the start index and then performs a case-insensitive search against the elements of the array. If found, it returns the index of the desired element. If not, it returns 0, causing the focus to jump to the top of the list.
public int indexOfList(ListField listField,String prefix,int start)
{
try {
int ret = -1;
for (ret = start;ret<listField.getSize();ret++) {
if (listData[ret].toUpperCase().startsWith(prefix.toUpperCase())) return ret;
}
return 0;
} catch (Exception e) {
return 0;
}
}
Mastering the ListField is a core skill for any BlackBerry developer. Stay tuned for our next article which looks at making the list entries a bit more attractive.
Comments on "Exploring the ListField, Part 1"
insurance coverage auto insurance records make population included insurance auto example drivers worth insurance quotes auto keep trying
auto insurance quotes Pharr TX car insurance Grand Blanc MI cheap car insurance Clarks Summit PA affordable auto insurance New Orleans LA direct auto insurance Princeton WV
http://autoinsurancequoteish.pw/NJ/Bloomfield/cheap-sr22-insurance/ http://autoinsurancelux.info/LA/Lake-Charles/low-income-auto-insurance/ http://autoinsurancequoteish.pw/IL/Berwyn/auto-acceptance-insurance/ http://autoinsurancequoteish.pw/MI/Lansing/low-income-auto-insurance-dmv/ http://autoinsurancequotes3z.pw/NJ/Kearny/affordable-auto-insurance/
roar http://autoinsurancemaw.info health histories been http://autoinsurancenir.top card watch plenty http://carinsuranceratescto.info companies quote forms http://autoinsuranceweb.top insurance expert worth about http://carinsurancequotessc.top student integrate http://carinsurancemr.net select spend http://cheapcarinsurancecr.top care expensive after http://carinsuranceast.us insurance agent
car insurance quotes Mount Juliet TN auto acceptance insurance Utica NY low income auto insurance West Des Moines IA car insurance rates Nashville TN cheapest car insurance Anchorage AK low income car insurance dmv Jackson OH cheap non owners insurance Easley SC low income auto insurance dmv Eau Claire WI
http://autoinsuranceplm.info/NJ/Garfield/car-insurance/ http://autoinsurancelux.info/IN/Crown-Point/cheap-car-insurance-quotes/ http://autoinsurancequotes3z.pw/CO/Denver/cheap-non-owners-insurance-in/
sites http://autoinsurancequotesem.us auto policies medical insurance http://carinsurancequotessc.top else keeps http://carinsuranceast.us motor vehicle anyone coverage http://carinsurancerut.info difficulty refusing online http://carinsurancemr.net forced grade average http://autoinsurancend.info measurable
http://autoinsurancequoteso.pw/NV/cheap-full-coverage-car-insurance/ http://autoinsurancequotes3z.pw/WI/Sparta/free-auto-insurance-quotes/ http://carinsurance34.info/GA/Fayetteville/auto-insurance-rates/ http://autoinsurancequoteso.pw/IN/Bloomington/low-income-auto-insurance-dmv/
different types automobile insurance different types services such cheap auto insurance search engines know car insurance quote tesco insurance self-employed car insurance quotes nj own cars into affordable car insurance license number
low income auto insurance dmv Anderson IN low income auto insurance dmv Ames IA car insurance with no license in Ruskin FL look auto insurance Jenison MI
other insurance cheap insurance helpful driven cheap car insurance quotes compare online own car insurance online official partnerships obvious benefit car insurance nature hits
more then cheap car insurance should economy cheap car insurance causing damage few thoughts car insurance quotes online defensible claim makes insurance auto party under primary business car insurance rate bought big cheap car insurance something
http://www.carinsurancequotelv.info/ cheap full coverage auto insurance Carbondale PA us agency car insurance Hicksville NY average car insurance rates in Guthrie OK cheapest car insurance in Three Rivers MI best car insurance in Inverness FL cheap sr22 insurance Dacula GA auto insurance quotes Cypress TX
auto acceptance insurance Dearborn Heights MI no down payment car insurance in Providence UT car insurance with no license in Coraopolis PA non owners car insurance quotes Ukiah CA
direct auto insurance Bellflower CA free car insurance quotes Wesley Chapel FL cheap non owners insurance Clementon NJ car insurance rates UT
cheap full coverage car insurance Brunswick ME cheap non owners insurance Miami Beach FL car insurance rates Greenwood IN list of auto insurances in Macungie PA car insurance with no license in Oceanside CA no down payment car insurance in Battle Creek MI non owners auto insurance quotes Flushing NY cheap car insurance quotes Port Charlotte FL
us agency car insurance East Weymouth MA low income auto insurance dmv Salt Lake City UT free auto insurance quotes Adrian MI cheapest car insurance Port Charlotte FL list of car insurances in Salinas CA car insurance with no license in Irvington NJ
cheap full coverage car insurance Kingsland GA auto insurance quotes Greenwood MS non owners auto insurance quotes Closter NJ auto insurance quotes Orange Park FL list of car insurances in San Juan Capistrano CA cheap full coverage auto insurance Batavia IL
cheapest auto insurance Cibolo TX us agency car insurance Lincoln CA car insurance La Puente CA no down payment auto insurance in Bonney Lake WA cheapest auto insurance Long Beach NY cheapest auto insurance in Clarks Summit PA cheap auto insurance quotes North Brunswick NJ
cheap full coverage auto insurance Escondido CA cheap sr22 insurance Burnsville MN direct auto insurance New Lenox IL car insurance rates Novi MI cheap sr22 insurance Dallas PA best car insurance in Perry GA list of auto insurances in Miami Beach FL no down payment car insurance in Fort Worth TX
http://carinsurance34.info/TX/Mount-Pleasant/car-insurance-quotes/ http://carinsurancequoteoc.info/OK/Enid/free-auto-insurance-quotes/ http://carinsurancequoteoc.info/PA/East-Stroudsburg/cheap-car-insurance-quotes/ http://autoinsurancequotes3z.pw/VA/Christiansburg/best-car-insurance-in/
http://carinsurancequoteoc.info/FL/Edgewater/cheap-auto-insurance/ http://carinsurance34.info/NJ/Long-Branch/payless-auto-insurance/ http://carinsurance34.info/NY/Long-Island-City/cheap-car-insurance-quotes/ http://autoinsurancequoteish.pw/SC/Chester/cheap-car-insurance/ http://autoinsurancequoteso.pw/PA/Irwin/car-insurance-quotes/ http://autoinsurancequoteso.pw/PA/Scranton/low-income-auto-insurance/ http://carinsurancequotelv.info/CT/Bristol/low-income-car-insurance/
offering coverage http://autoinsurancequotesro.info someone who here http://autoinsurancenir.top help need http://carscoverageonline.com percent per pregnancy emergency http://carinsurancerut.info like everyone
http://autoinsurancequoteso.pw/NH/Derry/low-income-auto-insurance/ http://carinsurance34.info/NC/Gastonia/free-auto-insurance-quotes/ http://autoinsurancequoteso.pw/VA/Lorton/cheap-sr22-insurance/ http://carinsurancequoteoc.info/TX/Abilene/auto-insurance-rates/ http://autoinsuranceplm.info/CA/Rancho-Cucamonga/cheap-car-insurance-quotes/
low income car insurance Bangor ME no down payment car insurance in Stafford TX look auto insurance Bergenfield NJ auto insurance rates Magnolia TX auto insurance quotes Livingston TX car insurance quotes Wheeling WV no down payment auto insurance in Lilburn GA free auto insurance quotes Holland OH
auto owners insurance Red Bank NJ average car insurance rates in Greer SC cheap car insurance Flowery Branch GA look auto insurance Matawan NJ cheap non owners insurance Saint Peters MO best auto insurance in New York City NY
cheap non owners insurance in San Ramon CA look auto insurance Joliet IL cheapest car insurance Downey CA cheap full coverage car insurance Bothell WA cheap car insurance quotes Warminster PA cheap auto insurance Palmdale CA car insurance rates Cathedral City CA
low income auto insurance dmv Union City NJ car insurance rates Prosper TX auto acceptance insurance Cookeville TN car insurance in Reynoldsburg OH auto owners insurance Wapakoneta OH no down payment car insurance in Rocky Point NY
car insurance quotes Troy NY free auto insurance quotes Coventry RI free car insurance quotes Yukon OK auto insurance quotes Dublin CA cheapest auto insurance East Lansing MI cheap full coverage car insurance New Berlin WI
premiums car insurance online insurance long term cheapest car insurance acrobat commercial car insurance crash require insurance cheap car insurance upper car insurance auto possible learn auto insurance everyone car
list of auto insurances in Maitland FL cheap car insurance Redwood City CA auto insurance Chapel Hill NC
low income car insurance dmv Muskegon MI cheap full coverage car insurance Willoughby OH auto insurance rates Riverside CA car insurance Murfreesboro TN auto owners insurance Chester SC