0

I'm new to blackberry development. There is this question I've come across several times, which is "How to get the Selected item as string". The answers that were given did not fully answer the question:

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...
     }
 }

The point is how can I get the selected text and "do something". Imagine I want to send the items as a string to a server via post. How would I do that in code?

Thank you! Michael.

Leigh
  • 28,765
  • 10
  • 55
  • 103
michael92
  • 61
  • 1
  • 1
  • 10

1 Answers1

1

These are really (at least) two different things.

To get the selected text, see this answer

To send a HTTP POST request, see this other answer

Normally, it's also bad to make network requests on the UI thread (which is what will callback your onSelect() method). So, it would be better to take the HTTP POST code from the second answer, and put it inside the run() method of a Runnable object you could run on a background thread. Something like this:

private class PostRequest implements Runnable {

  private String _postParam;

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

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

And use it like this:

  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();
        }
     }
  };
Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • worked wonderfully! :) all i need now is to display the results on a new screen in the form of a lists that can be selected. any help on that too? – michael92 Jun 22 '12 at 10:05
  • @michael92, so if this worked to solve the question you described here, the thing to do is press the little Accept checkbox next to this answer, to mark it as solved. then, take the next part of your task, and ask a new question. that is, if you can't simply find the answer by googling :) – Nate Jun 22 '12 at 10:18