Codenameone 代码名1-选择了autocompletetextfield getitem

Codenameone 代码名1-选择了autocompletetextfield getitem,codenameone,Codenameone,我有两个问题: 1) 首先,我想在AutoCompleteTextField中准确地添加一个映射到DefaultListModel中,然后我想在我的AutoCompleteTextField 2) 在AutoCompleteTextField中选择元素时,如何获取文本 Form hi = new Form("Auto Complete", new BoxLayout(BoxLayout.Y_AXIS)); AutoCompleteTextField ac = new Au

我有两个问题:

1) 首先,我想在
AutoCompleteTextField
中准确地添加一个映射到
DefaultListModel
中,然后我想在我的
AutoCompleteTextField

2) 在
AutoCompleteTextField
中选择元素时,如何获取文本

    Form hi = new Form("Auto Complete", new BoxLayout(BoxLayout.Y_AXIS));
        AutoCompleteTextField ac = new AutoCompleteTextField("Short", "Shock", "Sholder", "Shrek0", "3asna", "niazra");
    ac.setMinimumElementsShownInPopup(5);
    //final DefaultListModel<Map<String,Object>> options = new DefaultListModel<>();
    final DefaultListModel<String> options = new DefaultListModel<>();

    AutoCompleteTextField an = new AutoCompleteTextField(options);
    hi.add(an);
    ac.addListListener(a -> {
        List<Object> ls = new List<>();
        System.out.println("i want to display the text selected");

    });
    hi.add(ac);
    hi.show();
Form hi=新表单(“自动完成”,新的BoxLayout(BoxLayout.Y_轴));
AutoCompleteTextField ac=新的AutoCompleteTextField(“Short”、“Shock”、“Sholder”、“Shrek0”、“3asna”、“niazra”);
ac.SetMinimumElementsShowInPopup(5);
//最终DefaultListModel选项=新建DefaultListModel();
最终DefaultListModel选项=新建DefaultListModel();
AutoCompleteTextField an=新的AutoCompleteTextField(选项);
嗨,添加(an);
ac.addListListener(a->{
列表ls=新列表();
System.out.println(“我想显示所选的文本”);
});
hi.add(ac);
嗨,show();

当您在AutoCompleteTextField的建议框中选择一个项目时,该项目的文本将复制到AutoCompleteTextField的TextField部分,但这仅发生在ListEvent之后

要实现此行为,请在DefaultListModel上使用选择侦听器:

    Form hi = new Form("ACTF", new BoxLayout(BoxLayout.Y_AXIS));

    DefaultListModel<String> defList = new DefaultListModel<>("Red", "Green", "Blue", "Orange");
    AutoCompleteTextField tf1 = new AutoCompleteTextField(defList);

    defList.addSelectionListener((oldid, newid)-> Log.p(defList.getItemAt(newid)));

    hi.add(tf1);
    hi.show();
Form hi=新表单(“ACTF”,新的BoxLayout(BoxLayout.Y_轴));
DefaultListModel defList=新的DefaultListModel(“红色”、“绿色”、“蓝色”、“橙色”);
AutoCompleteTextField tf1=新的AutoCompleteTextField(defList);
addSelectionListener((oldid,newid)->Log.p(defList.getItemAt(newid));
hi.add(tf1);
嗨,show();
我不知道为什么,它在显示表单后出现了两次,但在显示表单后效果非常好

编辑:如果要在屏幕上显示文本,必须使用以下内容:

    Form hi = new Form("ACTF", new BoxLayout(BoxLayout.Y_AXIS));

    Label text = new Label("Selected text");

    DefaultListModel<String> defList = new DefaultListModel<>("Red", "Green", "Blue", "Orange");
    AutoCompleteTextField tf1 = new AutoCompleteTextField(defList);

    defList.addSelectionListener((oldid, newid)-> {
        text.setText(defList.getItemAt(newid));
        hi.revalidate();
    });
    hi.add(text);
    hi.add(tf1);
    hi.show();
Form hi=新表单(“ACTF”,新的BoxLayout(BoxLayout.Y_轴));
标签文本=新标签(“选定文本”);
DefaultListModel defList=新的DefaultListModel(“红色”、“绿色”、“蓝色”、“橙色”);
AutoCompleteTextField tf1=新的AutoCompleteTextField(defList);
defList.addSelectionListener((旧ID,新ID)->{
setText(defList.getItemAt(newid));
hi.revalidate();
});
添加(文本);
hi.add(tf1);
嗨,show();
编辑2:带有链接地图的示例:

    Form hi = new Form("ACTF", new BoxLayout(BoxLayout.Y_AXIS));

    Map testMap = new HashMap<String, String>();
    testMap.put("Red", "Roses are red");
    testMap.put("Green", "Grass is green");
    testMap.put("Blue", "Sky is blue");
    testMap.put("Orange", "Apricots are orange");

    Label text = new Label("Selected text");



    DefaultListModel<String> defList = new DefaultListModel<>(testMap.keySet());
    AutoCompleteTextField tf1 = new AutoCompleteTextField(defList);

    defList.addSelectionListener((oldid, newid)-> {
        text.setText((String) testMap.get(defList.getItemAt(newid)));
        hi.revalidate();
    });
    hi.add(text);
    hi.add(tf1);
    hi.show();
Form hi=新表单(“ACTF”,新的BoxLayout(BoxLayout.Y_轴));
Map testMap=newhashmap();
testMap.put(“红色”、“玫瑰是红色”);
testMap.put(“绿色”,“草是绿色的”);
testMap.put(“蓝色”,“天空是蓝色的”);
放置(“橙色”,“杏子是橙色”);
标签文本=新标签(“选定文本”);
DefaultListModel defList=新的DefaultListModel(testMap.keySet());
AutoCompleteTextField tf1=新的AutoCompleteTextField(defList);
defList.addSelectionListener((旧ID,新ID)->{
text.setText((String)testMap.get(defList.getItemAt(newid));
hi.revalidate();
});
添加(文本);
hi.add(tf1);
嗨,show();

要在何处显示所选文本?在设备的屏幕上?这里,我的代码将文本发送到调试控制台,就像System.out.println一样。检查示例的编辑版本。感谢它能工作,但我现在想在AutoMautoCompleteTextField中添加一个映射,因为当我选择要获取id的项时,我不知道您是否理解我这里是一个示例final DefaultListModel options=new DefaultListModel();AutoCompleteTextField an=新的AutoCompleteTextField(选项);无法将贴图添加到默认列表模型。列表模型使用一个数组,该数组将从零开始的索引映射到一个值。如果要跟踪对象的字符串ID,您需要保留一个与列表模型索引相同的键对应列表,或者将输入值放入也存储您感兴趣的键的对象中。请您给我举个例子,因为AutoCompleteTextField不支持mapKeep a map变量,然后在action listener中只需执行
map.get即可(autoComplete.getText())