Wrap AutoCompleteField List Item

2020-05-07 18:25发布

My apps has AutoCompleteField that hold long text more than 100 Characters, if I use regular AutoCompleteField I cant read the rest of data.

Cant read the rest item description

How can I make the text wrap into 2 or more lines in the autocompletefield options ? I try using '\r'+'\n' and '\n', its not giving new line. setting it size and also set row height doesnt give me the result I wanted

AutoCompleteField autoCustomer = new AutoCompleteField(custList, style);
autoCustomer.getListField().setSize(20);
autoCustomer.getListField().setRowHeight(100);

1条回答
我命由我不由天
2楼-- · 2020-05-07 18:49

If I was you I would override drawListRow and draw the text using drawText which will give me total control on how the row should look. Try adapting your code to behave like this

AutoCompleteField autoCompleteField = new AutoCompleteField(
                filterList, AutoCompleteField.LIST_STATIC) {

            public void drawListRow(ListField listField, Graphics g,
                    int index, int y, int width) {

        BasicFilteredListResult result = (BasicFilteredListResult) (autoCompleteField
                .get(listField, index));
        if (result != null) 
        {
                          //Draw text here
        }
            }

            public void onSelect(Object selection, int type) {
                super.onSelect(selection, type);
                if (selection != null) {

                    BasicFilteredListResult result = (BasicFilteredListResult) this
                            .getSelectedObject();
                    handleResult((String) result._object);

                } else {
                    Dialog.alert(Resource
                            .getString(PLEASE_PICK_A_VALID_NAME));
                    return;
                }
            }
        };

IF you want to wrap your text you can use the following method

// Handy method to wrap text drawn with the specified font into rows with
// the max width
// Found here:
// http://supportforums.blackberry.com/t5/Java-Development/Can-drawText-wrap-text-into-multiple-lines/m-p/499901
public static String[] wrapText(String text, Font f, int maxWidth) {
    Vector result = new Vector();
    if (text == null)
        return new String[] {};

    boolean hasMore = true;

    // The current index of the cursor
    int current = 0;

    // The next line break index
    int lineBreak = -1;

    // The space after line break
    int nextSpace = -1;

    while (hasMore) {
        // Find the line break
        while (true) {
            lineBreak = nextSpace;
            if (lineBreak == text.length() - 1) {
                // We have reached the last line
                hasMore = false;
                break;
            }
            nextSpace = text.indexOf(' ', lineBreak + 1);
            if (nextSpace == -1)
                nextSpace = text.length() - 1;
            int linewidth = f
                    .getAdvance(text, current, nextSpace - current);
            // If too long, break out of the find loop
            if (linewidth > maxWidth)
                break;
        }
        String line = text.substring(current, lineBreak + 1);
        result.addElement(line);
        current = lineBreak + 1;
    }
    String[] resultArray = new String[result.size()];
    result.copyInto(resultArray);
    return resultArray;
}
查看更多
登录 后发表回答