0

我是黑莓开发的新手。我曾多次遇到过这个问题,即“如何将所选项目作为字符串获取”。给出的答案并没有完全回答这个问题:

AutoCompleteField autoCompleteField = new AutoCompleteField(filterList)
{
 public void onSelect(Object selection, int SELECT_TRACKWHEEL_CLICK) {
     ListField _list = getListField();
     if (_list.getSelectedIndex() > -1) {
         Dialog.alert("You selected: "+_list.getSelectedIndex());
         // get text selected by user and do something...
     }
 }

关键是如何获得选定的文本并“做某事”。想象一下,我想通过 post 将项目作为字符串发送到服务器。我将如何在代码中做到这一点?

谢谢!迈克尔。

4

1 回答 1

1

这些实际上(至少)是两种不同的东西。

要获取所选文本,请参阅此答案

要发送 HTTP POST 请求,请参阅this other answer

通常,在 UI 线程上发出网络请求也很糟糕(这将回调您的onSelect()方法)。因此,最好从第二个答案中获取 HTTP POST 代码,并将其放在可以在后台线程上运行的对象的run()方法中。Runnable像这样的东西:

private class PostRequest implements Runnable {

  private String _postParam;

  public PostRequest(String param) {
    _postParam = param;
  }

  public void run() {
    // make POST request here, using _postParam
  }
}

并像这样使用它:

  AutoCompleteField acf = new AutoCompleteField(list) {
     protected void onSelect(Object selection, int type) {
        super.onSelect(selection, type);
        if (selection != null) {
          String selectionAsString = getEditField().getText();
          Thread worker = new Thread(new PostRequest(selectionAsString));
          worker.start();
        }
     }
  };
于 2012-06-22T07:51:53.767 回答