将标签添加到BlackBerry ListField

时间:2010-11-30 10:50:31

标签: blackberry

我在BlackBerry上实施了ListField。如何在列表中添加3个标签?

1 个答案:

答案 0 :(得分:1)

遵循本教程: http://berrytutorials.blogspot.com/2009/11/create-custom-listfield-change.html

完成后,通过向列表中添加一些额外组件来修改扩展的ListField类(graphics.drawText(CALLBACK OBJECT,X,Y))。将String回调更改为您的类型(或只是一个数组)的对象,并具有更多元素的可用性。

扩展的LISTFIELD类中的绘制方法示例:

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