MobilityCountryBrowser

Mobile Country Browser with Databinding created with NetBeans Mobility

Software

You will need NetBeans IDE > 6.0 with all Mobility modules (means including End2End modules). How to get such IDE? 1, install full IDE 2, install NetBeans Mobility distribution and download Mobility End to End support with Plugin manager.

Scenario

This tutorial shows how to create mobile client to a web service. And how to display the returned data easily. This approach is using Java ME databinding library that is bundled with the NetBeans Mobility.


Steps

  1. Create new Mobility MIDP project.
    1. Name it CountryBrowser
    2. Don't create Hello midlet.
  2. Generate stubs for the mobile client
    1. New File > MIDP > Java ME Web Service Client, NEXT
    2. WSDL URL = http://www.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL
    3. click Retrieve WSDL button
    4. Check Generate Databinding Structures check box
    5. Finish - jsr172 stubs are generated
  3. Create UI
    1. New File > MIDP > Visual MIDlet
    2. Put List, WaitScreen, Form, Alert into Flow Designer
    3. Add commands and connect the screens as on the picture

http://wiki.netbeans.org/attach/MobilityCountryBrowser/binding-flow-design_MobilityCountryBrowser.jpg

  1. Create country form UI
    1. Open the form in screen designer
    2. Add 4 string items to the form
    3. Name the string items instances name, countryISO, capital, currencyISO
    4. Change the labels to look as on screenshot

http://wiki.netbeans.org/attach/MobilityCountryBrowser/binding-form-screen_MobilityCountryBrowser.jpg

  1. Create binding
    1. Go to source of the visual midlet
    2. In method getTask() add code
public void execute () throws Exception { // write task-execution user code here '''bind();'''//new code }
    1. Add calls of next() and previous() methods to commandAction method

if (command == next) {

// write pre-action user code here
   '''next();'''//new code
                                          
// write post-action user code here

} else if (command == previous) {

// write pre-action user code here
   '''previous();'''//new code
                                          
// write post-action user code here
}
    1. Show failure in alert. Change in commandAction method

} else if (displayable == waitScreen) { if (command == WaitScreen.FAILURE_COMMAND) {

               // write pre-action user code here
               '''getAlert().setString(getTask().getFailureMessage());'''//new code
               

switchDisplayable (getAlert (), getForm ());

           // write post-action user code here
}
  1. Create the missing methods - bind, next, previous
   protected void bind() throws RemoteException {
       CountriesIndexDS index = new CountriesIndexDS(); //index for navigation in aray
       DataBinder.registerDataSet( index, "index" ); //register the binder
       
       CountryInfoService service = new CountryInfoService_Stub(); //create mobile client stub
       DataBinder.registerDataSet(service.FullCountryInfoAllCountries(), "countries"); //call method of web service
       DataBinder.bind("countries.tCountryInfo[Index.index].sName",
               new StringItemBindingProvider(), getName(), StringItemBindingProvider.FEATURE_TEXT); //bind UI and the data structure
       DataBinder.bind("countries.tCountryInfo[Index.index].sCapitalCity",
               new StringItemBindingProvider(), getCapital(), StringItemBindingProvider.FEATURE_TEXT);//bind UI and the data structure
       DataBinder.bind("countries.tCountryInfo[Index.index].sCurrencyISOCode",
               new StringItemBindingProvider(), getCurrencyISO(), StringItemBindingProvider.FEATURE_TEXT);//bind UI and the data structure
       DataBinder.bind("countries.tCountryInfo[Index.index].sISOCode",
               new StringItemBindingProvider(), getCountryISO(), StringItemBindingProvider.FEATURE_TEXT);//bind UI and the data structure
   }
   private void next() {
       if (((Boolean) DataBinder.readValue("index.index < countries.tCountryInfo.length - 1")).booleanValue()) {
           DataBinder.writeValue("index.index", DataBinder.readValue("index.index + 1"));//change value of the index
       }
   }
   private void previous() {
       if (((Boolean) DataBinder.readValue("index.index > 0")).booleanValue()) {
           DataBinder.writeValue("index.index", DataBinder.readValue("index.index - 1"));//change value of the index
       }
}
  1. Fix imports
  2. Create missing index dataset class in the VisualMIDlet class
private static final class CountriesIndexDS implements DataSet { private static final String INDEX = "index"; private Long index = new Long(0); public Class getType(String arg0) throws DataBindingException { return Integer.class; } public Object getValue(String dataItemName) throws DataBindingException { if ("index".equals(dataItemName)) { return index; } throw new IllegalArgumentException("Invalid data item name"); } public void setValue(String dataItemName, Object value) throws DataBindingException { if ("index".equals(dataItemName)) { index = (Long) value; DataBinder.fireDataSetChanged(this, INDEX); return; } throw new IllegalArgumentException("Invalid data item name"); } public void setAsString(String dataItemName, String value) throws DataBindingException { if ("index".equals(dataItemName)) { index = new Long(Long.parseLong(value)); DataBinder.fireDataSetChanged(this, INDEX); return; } throw new IllegalArgumentException("Invalid data item name"); } public boolean isReadOnly(String arg0) throws DataBindingException { return false; } }
  1. Run project
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo