2010-11-30 101 views

回答

1

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

完成後,通過添加一些額外的組件列表(graphics.drawText(回調對象,X,Y))修改擴展ListField類。將字符串回調更改爲您類型的對象(或者只是一個數組),並提供更多元素。

例paint方法INSIDE擴展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); 
}