开发者

Pass a parameter from searchfield in blackberry

I'm new to Blackberry Development and i have to implement a search functionality. It is supposed to be like when i input something in the search field and p开发者_开发知识库ress the search button it should pass that string to a url as a parameter. Can anyone put me in right direction so as i can understand how to implement it.


If you want the searching to be done on a remote server, and just want the user to be able to enter a search term, then you'll first want a manger with two fields, such as a BasicEditField and a ButtonField. Then you'll want to hook up the button field to submit a request when it is clicked (I assume from your description that is http, but it could be anything). If your button field is stored in a buttonField variable, and your edit field is stored in your editField variable, and you have a method named sendRequest that takes a parameter and sends a request to the url, then you might have:

buttonField.setChangeListener(new FieldChangeListener() {
    public void fieldChanged (Field field, int context) {
        (new Thread() {
            public void run() {
                sendRequest(editField.getText());
            }
        }).start();
    }
});

This might look a little complicated, but it breaks down pretty easily:

  • By calling setChangeListener, we ensure that the fieldChanged method will be called whenever the button is pressed
  • When fieldChanged is called, we will be in the UI thread. We don't want to lock the UI while we make the request, so you should send the request in a new thread.
  • getText gets the current text that the user has entered, and passes it to your sendRequest method

The sendRequest method should send the request to the url you mentioned. This will differ depending on how old devices you need to support - BlackBerry introduced a new networking API in 5.0 and expanded on it in 6.0, that simplifies network communications significantly.

I'm assuming you'll be parsing some sort of response that holds the search results. In this case, you'll also want a manager (such as a VerticalFieldManager) to store those results. Then you would add a field for every result that you wanted to show. You'll need to be careful to be holding the UI event lock when doing this, however, since it will probably be executing in a background thread. For example, you might have:

public void addResult(String result) {
    synchronized(Application.getEventLock()) {
        searchResults.add(new LabelField(result));
    }
}

This would just show results in a label field, but you could obviously have more complex fields if you wanted more complicated behavior or more complicated UI.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜