Troubleshooting guide
34
BlackBerry Java Development Environment Development Guide
Code sample: Creating a custom list
Example: SampleListFieldCallback.java
/**
* SampleListFieldCallback.java
* Copyright (C) 2001-2005 Research In Motion Limited. All rights reserved.
*/
package com.rim.samples.docs.listfields;
import java.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
public class SampleListFieldCallback extends UiApplication {
private ListField myList;
public static void main(String[] args) {
SampleListFieldCallback app = new SampleListFieldCallback();
app.enterEventDispatcher();
}
private static 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 indexOfList(ListField list, String p, int s) {
return listElements.indexOf(p, s);
}
public int getPreferredWidth(ListField list) {
return Graphics.getScreenWidth();
}
public void insert(String toInsert, int index) {
listElements.addElement(toInsert);
}
public void erase() {
listElements.removeAllElements();
}
}
public SampleListFieldCallback() {
MainScreen mainScreen = new MainScreen();
myList = new ListField();
ListCallback myCallback = new ListCallback();
myList.setCallback(myCallback);
String fieldOne = “ListField one”;
String fieldTwo = “ListField two”;
String fieldThree = “ListField three”;
myList.insert(0);
myCallback.insert(fieldOne, 0);