【问题标题】:Codename One - Modify AutoCompleteTextField data in runtime代号一 - 在运行时修改 AutoCompleteTextField 数据
【发布时间】:2019-01-18 00:06:45
【问题描述】:

我有一个 AutoCompleteTextField,我想在运行时更改它的选项。 问题是我不知道如何显示更改。

这是我尝试过的一个非常简单的示例:

public void start() {
    if(current != null){
        current.show();
        return;
    }
    Form form = new Form( new FlowLayout() );

    String [] list2 = { "option A", "option B"};
    AutoCompleteTextField textField2 = new AutoCompleteTextField( list2 );
    form.add( textField2 );

    form.show();

    list2 =  new String [2];
    list2 [0] = "X";
    list2 [1] = "D";
    textField2 = new AutoCompleteTextField( list2 );

    form.show();
}

我总是显示“选项 A”和“选项 B”。我已经试过for.repaint(), textField2.repaint()...

任何帮助将不胜感激。

【问题讨论】:

    标签: codenameone


    【解决方案1】:

    试试这个:

    final DefaultListModel<String> options = new DefaultListModel<>();
    AutoCompleteTextField ac = new AutoCompleteTextField(options) {
    
        @Override
        protected boolean filter(String text) {
            if(text.length() == 0) {
                return false;
            }
            String[] l = changeOptions(text);
            if(l == null || l.length == 0) {
                return false;
            }
            options.removeAll();
            for(String s : l) {
                options.addItem(s);
            }
            return true;
        }
    
    };
    
    
    private String[] changeOptions(String text){
        String[] list2 =  new String [2];
        if(<some_condition_with_text>){
            list2 [0] = "option A";
            list2 [1] = "option B";
            return list2;
        }
        if(<some_other_condition_with_text>){
            list2 [0] = "X";
            list2 [1] = "D";
            return list2;
        }
    }
    

    看看https://www.codenameone.com/javadoc/com/codename1/ui/AutoCompleteTextField.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多