In this lesson, you add the following controls to the screen:
You then add code to insert the name in the text field and refresh the list.
Add the controls to the screen.
Add the following code before calling the getDataAccess method.
// Add an edit field for entering new names _nameEditField = new EditField( "Name: ", "", 50, EditField.USE_ALL_WIDTH ); add (_nameEditField ); // Add an ObjectListField for displaying a list of names _nameListField = new ObjectListField(); add( _nameListField ); // Add a menu item addMenuItem(_addToListMenuItem); // Create database and connect try{ _da = DataAccess.getDataAccess(); |
Add class-level declarations for _nameEditField and _nameListField. Also, define a _addToListMenuItem MenuItem with a run method (empty for now). These declarations belong next to the declarations of _statusLabel and _da.
private EditField _nameEditField; private ObjectListField _nameListField; private MenuItem _addToListMenuItem = new MenuItem("Add", 1, 1){ public void run() { // TODO } }; |
Recompile the application and confirm that it runs.
Add the following methods and objects to your application:
Add the DataAccess method to insert a row into a table:
public void insertName( String name ){ try{ Value nameID = _conn.createUUIDValue(); String sql = "INSERT INTO Names( ID, Name ) VALUES ( ?, ? )"; PreparedStatement ps = _conn.prepareStatement(sql); ps.set(1, nameID ); ps.set(2, name ); ps.execute(); _conn.commit(); } catch( ULjException uex ){ System.out.println( "ULjException: " + uex.toString() ); } catch( Exception ex ){ System.out.println( "Exception: " + ex.toString() ); } } |
Add the class that holds a row of the Names table. The toString method is used by the ObjectListField control.
package myapp; class NameRow { public NameRow( String nameID, String name ) { _nameID = nameID; _name = name; } public String getNameID(){ return _nameID; } public String getName(){ return _name; } public String toString(){ return _name; } private String _nameID; private String _name; } |
Add the DataAccess method to read the rows from the table into a Vector of objects:
public Vector getNameVector(){ Vector nameVector = new Vector(); try{ String sql = "SELECT ID, Name FROM Names"; PreparedStatement ps = _conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while ( rs.next() ){ String nameID = rs.getString(1); String name = rs.getString(2); NameRow nr = new NameRow( nameID, name); nameVector.addElement(nr); } } catch( ULjException uex ){ System.out.println( "ULjException: " + uex.toString() ); } catch( Exception ex ){ System.out.println( "Exception: " + ex.toString() ); } finally{ return nameVector; } } |
Add the user interface methods to the HomeScreen class. Following is the method to refresh the list of names:
public void refreshNameList(){ //Clear the list _nameListField.setSize(0); //Refill from the list of names Vector nameVector = _da.getNameVector(); for( Enumeration e = nameVector.elements(); e.hasMoreElements(); ){ NameRow nr = ( NameRow )e.nextElement(); _nameListField.insert(0, nr); } } |
Call the refreshNameList method before the end of the HomeScreen constructor so that the list is filled when the application starts.
// Fill the ObjectListField this.refreshNameList(); |
Add a HomeScreen method that is used to add a row to the list:
private void onAddToList(){ _da.insertName(_nameEditField.getText()); this.refreshNameList(); _nameEditField.setText(""); } |
Call this method from within the run method of the _addToListMenuItem MenuItem (which currently says //TODO):
public void run() { onAddToList(); } |
Compile and run the application.
If you need to reset the simulator to a clean state, choose Erase Simulator File from the BlackBerry JDE File menu (not the Simulator menu), and erase the items in the submenu. If you reset the simulator this way, you must re-import the UltraLiteJ.cod file before running your application again.
Discuss this page in DocCommentXchange. Send feedback about this page using email. |
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |