title: Repaining listfield on editfield change Input
body: <p>I am using Google place API in my application for searching location. When user input text in edit field then the API called and resulted output will shown in a list.</p>
  
  <p>I implemented it successfully but the problem is that each time edit field text changes, the list is not repainting and output is added to the <strong>end</strong> of the list. I want every time the text changes in the edit text field, the list must remove its previous content that are invalid.</p>
  
  <p>This can be seen in pictures:</p>
  
  <p><img src="https://i.stack.imgur.com/fb1hf.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/P6ySd.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/03QRW.png" alt="enter image description here"></p>
  
  <p>For Implementing this, I have written this code:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>public final class MyScreen extends MainScreen {
  /**
   * Creates a new MyScreen object
   */
  private Vector _listElements;
  ListField list;
  JSONObject[] jsonobject;
  EditField editfield;
  String url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=";
  String[] locationName;
  VerticalFieldManager verticalFieldManager = new VerticalFieldManager();
  
  public MyScreen() {
      ButtonField search = new ButtonField("Search");
      _listElements = new Vector();
      list = new ListField();
      ListCallback _callback = new ListCallback(this);
  
      // Set the displayed title of the screen
      setTitle("Search Edit Field");
      editfield = new EditField();
      editfield.setChangeListener(new FieldChangeListener() {
          public void fieldChanged(Field field, int context) {
              UiApplication.getUiApplication().invokeLater(new Runnable() {
                  public void run() {
                      list.invalidate();
                      createField();
                  }
              });
          }
      });
      list.setCallback(_callback);
      add(editfield);
  
      add(new SeparatorField());
  
      verticalFieldManager.add(list);
      add(verticalFieldManager);
  
  }
  
  protected void createField() {
      ShowList();
      reloadList();
  
  }
  
  private void reloadList() {
  
      list.setSize(_listElements.size());
  
  }
  
  class ListCallback implements ListFieldCallback {
      MyScreen listDemoScreen;
  
      public ListCallback(MyScreen listDemoScreen) {
          this.listDemoScreen = listDemoScreen;
  
      }
  
      public void drawListRow(ListField list, Graphics g, int index, int y,
              int w) {
  
          String text = (String) _listElements.elementAt(index);
          list.setRowHeight(getFont().getHeight());
  
          g.drawText(text, 0, y, 0, -1);
      }
  
      public Object get(ListField list, int index) {
          return _listElements.elementAt(index);
      }
  
      public int indexOfList(ListField list, String prefix, int string) {
          return _listElements.indexOf(prefix, string);
      }
  
      public int getPreferredWidth(ListField list) {
          return Display.getWidth();
      }
  
  }
  
  protected void ShowList() {
      HttpConnection httpConn;
  
      InputStream in;
      ConnectionFactory connFact = new ConnectionFactory();
      ConnectionDescriptor connDesc;
      String response;
      String fieldText = editfield.getText();
      connDesc = connFact.getConnection(url + fieldText
              + "%@&amp;sensor=true&amp;key=xxxxxxxxxxxxx"
              + ConnectionType.getConnectionType());
      if (connDesc != null) {
          httpConn = (HttpConnection) connDesc.getConnection();
          try {
              int responseCode = httpConn.getResponseCode();
              if (responseCode == HttpConnection.HTTP_OK) {
                  in = httpConn.openInputStream();
                  StringBuffer buf = new StringBuffer();
                  int read = -1;
                  while ((read = in.read()) != -1)
                      buf.append((char) read);
  
                  response = buf.toString();
                  try {
                      JSONObject object = new JSONObject(response);
                      JSONArray ar = object.getJSONArray("predictions");
                      jsonobject = new JSONObject[ar.length()];
                      locationName = new String[ar.length()];
                      list.invalidate();
                      for (int i = 0; i &lt; ar.length(); i++) {
                          jsonobject[i] = ar.getJSONObject(i);
  
                          _listElements.addElement(jsonobject[i]
                                  .getString("description"));
  
                      }
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      } else {
          Dialog.alert("Connection not succeded");
      }
  }
  
  protected boolean onSavePrompt() {
      return true;
      }
  }
  </code></pre>
  
  <p><strong>Update and Solution:</strong></p>
  
  <p>only modify this and this rest is working fine.  As Peter Suggests, we can also put a <code>Thread.sleep(time);</code> in order to get the UI not blocked:</p>
  
  <pre class="lang-java prettyprint-override"><code>UiApplication.getUiApplication().invokeLater(new Runnable() {
      public void run() {
        _listElements.removeAllElements();
          createField();
      }
  });
  </code></pre>