Codenameone 列表添加功能

Codenameone list adding functionality

我目前正忙于开发一个 Codenameone 应用程序,它要求我通过单击按钮添加项目列表,就像在任务列表中添加任务一样。我将如何处理这个?我对此有点陌生。请帮忙。

这是一个简短的例子:

Form form = new Form("List Example");  //Create Form

Button button = new Button("PRESS ME"); //Create Button
form.add(button);  // add button to Form

List myList = new List<>(); //Create List
form.add(myList);  //add List to Form

// Create an Array of Elements 
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 20; i++)
  {
    arrayList.add("Elemnt " + i);
  }

// Create ListModel
DefaultListModel<String> listModel = new DefaultListModel<>(arrayList);

// Add Button ActionListner
button.addActionListener(new ActionListener<ActionEvent>()
{
  public void actionPerformed(ActionEvent arg0)
  {
    myList.setModel(listModel);  //add ListModel to List
    form.repaint(); 
  }
});

form.show();