Blackberry ListField with custom image to highlight and 3 strings in single row.

This is the code in Blackberry ListField for showing multiple entries in a list. Each entry 3 distinct strings, with different fonts sizes. The entry is highlighted by using a custom colored image. User can highlight the list item and hit menu key to see the details about the highlighted entry as well.

package mypackage;

import java.util.Vector;

import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.decor.BackgroundFactory;

/**
*
* @author swapnil.chaudhari Date:Jan 23, 2012
*/
public class TeamListScreen extends MainScreen implements ListFieldCallback,
FieldChangeListener {

// 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 Font[] _arrFonts = new Font[]{Font.getDefault().derive(Font.PLAIN, 11),
Font.getDefault().derive(Font.PLAIN, 14),
Font.getDefault().derive(Font.PLAIN, 20)};
private Vector _listGirls = new Vector(5);
//0x777bf85d25419768L
public static ResourceBundle _resources = ResourceBundle.getBundle(
0x777bf85d25419768L, "mypackage.resource");
private Bitmap _focusRow = Bitmap.getBitmapResource(_resources.getString(resourceResource.focus_image));
// 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
+ " "
+ _selectedPerson._lastName;
if (_selectedPersonIsABoy) {
name = name + " is a Boy";
} else {
name = name + " is a Girl";
}
Status.show(name);
}
}
};

public TeamListScreen() {
// Set up Dummy Data

/*
* _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");
Manager manager = getMainManager();
manager.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
ButtonField buttonField = new ButtonField(
"click to add new name",
ButtonField.CONSUME_CLICK);
buttonField.setChangeListener(this);
add(buttonField);
this.add(new LabelField("Boys"){
/* (non-Javadoc)
* @see net.rim.device.api.ui.component.LabelField#paint(net.rim.device.api.ui.Graphics)
*/
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
});
_lfBoys = new ListField(_listBoys.size()) {

protected void drawFocus(Graphics graphics, boolean on) {
// get the focus rect area
XYRect focusRect = new XYRect();
getFocusRect(focusRect);

boolean oldDrawStyleFocus = graphics
.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS);
try {
if (on) {
// set the style so the fields in the row will update its color accordingly
graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS,true);
int oldColour = graphics.getColor();
try{
if(_focusRow.getWidth()< getWidth() || _focusRow.getHeight()<getRowHeight()){
Bitmap blank = new Bitmap(getWidth(), getRowHeight());
_focusRow.scaleInto(blank, Bitmap.FILTER_BILINEAR);
_focusRow = blank;
}
graphics.drawBitmap(focusRect.x, focusRect.y, focusRect.width,
focusRect.height, _focusRow, 0, 0);
// set the color and draw the color
/*graphics.setColor(Color.ORANGERED);
graphics.fillRect(
focusRect.x,
focusRect.y,
focusRect.width,
focusRect.height);*/
} finally {
graphics.setColor(oldColour);
}
// to draw the row again
drawListRow(this,
graphics,
getSelectedIndex(),
focusRect.y,
focusRect.width);
}

} finally {
graphics.setDrawingStyle(
Graphics.DRAWSTYLE_FOCUS,
oldDrawStyleFocus);
}
}
};
int height=0;
for(int i=0;i<_arrFonts.length;i++){
height = height + _arrFonts[i].getHeight()+3;
}
height = height + 3;
_lfBoys.setEmptyString(null,DrawStyle.HCENTER);
_lfBoys.setRowHeight(height);
_lfBoys.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
//following line takes height of row as 3 times height of font
//_lfBoys.setRowHeight(-3);
_lfBoys.setCallback(this); // We manage the interaction!
this.add(_lfBoys);

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

}

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());
}
menu.add(_displayPersonMenu);
}

// Implemented Call Back Methods follow

// draw the curent row
public void drawListRow(ListField list, Graphics g, int index, int y,
int w) {
try {
if (index >= 0) {
//color used to draw the list strings
int drawColor = Color.GRAY;
g.setColor(drawColor);
g.drawLine(0, y, w, y);
drawColor = Color.WHITE;
g.setColor(drawColor);
Person personToDraw = (Person) this.get(list,
index);
g.setFont(_arrFonts[0]);
y=y+3;
g.drawText(personToDraw._firstName + " "
+ personToDraw._lastName, 0, y,
0, w);
y=y+_arrFonts[0].getHeight()+3;
g.setFont(_arrFonts[1]);
g.drawText(personToDraw._firstName, 0,y);
y=y+_arrFonts[1].getHeight()+3;
g.setFont(_arrFonts[2]);
g.drawText(personToDraw._lastName, 0, y );
}
} catch (Exception e) {
}
}

/*
* (non-Javadoc)
*
* @see
* net.rim.device.api.ui.Field#drawFocus(net.rim.device.api.ui.Graphics,
* boolean)
*/

// 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;
}
}

private int lastAddedIndex = 0;

/*
* (non-Javadoc)
*
* @see
* net.rim.device.api.ui.FieldChangeListener#fieldChanged(net.rim.device
* .api.ui.Field, int)
*/
public void fieldChanged(Field field, int context) {
_listBoys.addElement(new Person("Joe" + lastAddedIndex, "R"
+ lastAddedIndex, "Bloggs" + lastAddedIndex));
lastAddedIndex++;
long time = System.currentTimeMillis();
_lfBoys.setSize(lastAddedIndex, 0);
_lfBoys.invalidate();
time = System.currentTimeMillis() - time;
System.out.println("Swapnil:" + time);
}

}

Comments

Popular posts from this blog

Enhancing LLM Responses with Prompt Stuffing in Spring Boot AI

Automate Library Integration with Cursor's Agent Mode

Upload an image from Android to Google's app engine server