Wrap AutoCompleteField List Item

↘锁芯ラ 提交于 2019-12-02 15:17:57

问题


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

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:


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


来源:https://stackoverflow.com/questions/8995693/wrap-autocompletefield-list-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!