Add a label to a BlackBerry ListField
I have开发者_如何学运维 a implemented a ListField on BlackBerry. How do I add 3 labels to the list?
Follow this tutorial: http://berrytutorials.blogspot.com/2009/11/create-custom-listfield-change.html
After completed, modify the extended ListField class by adding some extra components to your list (graphics.drawText(CALLBACK OBJECT, X, Y)). Change the String callback to an object of your type(or just an Array) with the availability for more elements.
EXAMPLE OF THE PAINT METHOD INSIDE THE EXTENDED LISTFIELD CLASS:
public void paint(Graphics graphics) {
int width = (int) (300 * resizeWidthFactor);
// Get the current clipping region
XYRect redrawRect = graphics.getClippingRect();
// Side lines
// graphics.setColor(Color.GRAY);
// graphics.drawLine(0, 0, 0, redrawRect.height);
// graphics.setColor(Color.GRAY);
// graphics.drawLine(redrawRect.width-1, 0, redrawRect.width-1,
// redrawRect.height);
if (redrawRect.y < 0) {
throw new IllegalStateException("Error with clipping rect.");
}
// Determine the start location of the clipping region and end.
int rowHeight = getRowHeight();
int curSelected;
// If the ListeField has focus determine the selected row.
if (hasFocus) {
curSelected = getSelectedIndex();
} else {
curSelected = -1;
}
int startLine = redrawRect.y / rowHeight;
int endLine = (redrawRect.y + redrawRect.height - 1) / rowHeight;
endLine = Math.min(endLine, getSize() - 1);
int y = (startLine * rowHeight) + heightMargin;
// Setup the data used for drawing.
int[] yInds = new int[] { y, y, y + rowHeight, y + rowHeight };
int[] xInds = new int[] { 0, width, width, 0 };
// Set the callback - assuming String values.
ListFieldCallback callBack = this.getCallback();
// Draw each row
for (; startLine <= endLine; ++startLine) {
// If the line we're drawing is the currentlySelected line then draw the
// fill path in LIGHTYELLOW and the
// font text in Black.
//OBJECT OF OWN TYPE FOR MULTIPLE PARAMETERS
ProductDetails data = (ProductDetails) callBack.get(this, startLine);
String productDescription = "";
String errorDescription = "";
if (data.isError()) {
errorDescription = TextLineSplitter.wrapString1Line(data.getErrorMessage(), (int) ((300 - (2 * widthMargin)) * resizeWidthFactor), getFont());
} else {
productDescription = TextLineSplitter.wrapString1Line(data.getProductDesc(), (int) ((300 - (2 * widthMargin)) * resizeWidthFactor), getFont());
}
// Set differences by row (selected or not)
if (startLine == curSelected) {
graphics.setColor(Color.WHITE);
} else {
// Draw the odd or selected rows.
graphics.setColor(Color.BLACK);
}
// Set text values
if (!data.isError()) {
// If no error found
//FIRST LABEL
graphics.setFont(getFont().derive(Font.BOLD));
graphics.drawText("Result search " + Integer.toString(data.getSearchId()) + ":", widthMargin, yInds[0]);
graphics.drawText(data.getManufacturerItemIdentifier(), widthMargin + (int) (140 * resizeWidthFactor), yInds[0]);
//SECOND LABEL
graphics.setFont(getFont().derive(Font.PLAIN));
graphics.drawText(productDescription, widthMargin, yInds[0] + (int) (20 * resizeHeightFactor));
} else {
// Error found
graphics.setColor(Color.GRAY);
graphics.setFont(getFont().derive(Font.BOLD));
graphics.drawText("Result search " + Integer.toString(data.getSearchId()) + ":", widthMargin, yInds[0]);
graphics.setFont(getFont().derive(Font.PLAIN));
graphics.drawText(errorDescription, widthMargin, yInds[0] + (int) (20 * resizeHeightFactor));
}
// Bottom line
if (startLine == endLine) {
graphics.setColor(Color.GRAY);
graphics.drawLine(0, yInds[2] - (heightMargin + 1), (int) (300 * resizeWidthFactor), yInds[2] - (heightMargin + 1));
}
// Horizontal lines
graphics.setColor(Color.GRAY);
graphics.drawLine(0, yInds[0] - heightMargin, (int) (300 * resizeWidthFactor), yInds[0] - heightMargin);
// Assign new values to the y axis moving one row down.
y += rowHeight;
yInds[0] = y;
yInds[1] = yInds[0];
yInds[2] = y + rowHeight;
yInds[3] = yInds[2];
}
// super.paint(graphics);
}
精彩评论