Changes

Jump to: navigation, search
no edit summary
=== 3. Add Elements to on Main (First) Screen ===
3.1. Open <code>MyScreen.java</code> class.<br/>
3.2. Create class <code>Student</code> with the following fields:
<presource lang="java">
private String firstName;
private String lastName;
private String email;
</presource> 3.3. Add an array in three argument constructor: <codesource lang="java">MyScreen</code> class to work with data public Student(String firstName, String lastName, String email) { this.firstName = firstName; this.lastName = lastName; this.email = email; } <br/source> 3.4. The following elements are added to Add getter method for e-mail and override the <code>MyScreentoString()</code> constructor. Add a text field: from the Object class(it will be used to implement <code>Comparator<br/code>interface): <presource lang="java"> public String getEmail() { return email; AutoTextEditField atf = new AutoTextEditField} public String toString() { return firstName + "Student Name: ", "")+ lastName; } </presource> 3.5. Set the border Add class that will hold all Student objects and background for the text fieldmake necessary manipulations: <presource lang="java"> public class StudentList extends SortedReadableList implements KeywordProvider {  Border roundedBorder = BorderFactory.createRoundedBorderpublic StudentList(Vector students) { super(new XYEdgesStudentListComparator(5, 5, 5, 5), Border); loadFrom(students.STYLE_SOLIDelements()); }  Background solidBackground = BackgroundFactory/** * Adds a new element to the list. * @param element The element to be added.createSolidBackground */ void addElement(Color.LIGHTSTEELBLUEObject element) { doAdd(element); atf}   /** * A Comparator class used for sorting our Student objects by name. */ final static class StudentListComparator implements Comparator { /** * Compares two students by comparing their names in alphabetical order. * @see net.rim.device.api.util.setBorderComparator#compare(Object, Object) */ public int compare(Object o1, Object o2) { if (roundedBordero1 == null || o2 == null) { throw new IllegalArgumentException("Cannot compare null students"); atf } return o1.toString().compareTo(o2.setBackgroundtoString(solidBackground)); } }  addpublic String[] getKeywords(Object element) { if(element instanceof Student ) { return StringUtilities.stringToWords(element.toString(atf)); } return null; } }</presource> 3.6. Add a search buttonThe following elements are added to the <code>MyScreen()</code> constructor:<br/> <presource lang="java"> ButtonField _bf // A reference to the UiApplication instance for use in this class _app = app; // Obtain a reference to the UiApplication's KeywordFilterField _keywordFilterField =new ButtonField_app.getKeywordFilterField();</source> 3.7. Other elements are added inside the <code>ViewStudentApp</code> class. Add necessary private fields: <source lang="Searchjava")> private KeywordFilterField _keywordFilterField; private StudentList _studentList; add(_bf)private Vector _students; </presource> 3.78. Add the array of search field and students to the list field: <presource lang="java"> ObjectListField olf MyScreen _mainScreen = new ObjectListFieldMyScreen(this);  for _students = getFromList(int i); _studentList =0new StudentList(_students); i< ls  // Add our list to a KeywordFilterField object.length _keywordFilterField = new KeywordFilterField(); i++) { olf _keywordFilterField.insertsetSourceList(i_studentList, ls[i]_studentList);    _mainScreen.getFirstNamesetTitle() + " " + ls[i]_keywordFilterField.getLastNamegetKeywordField()); } _mainScreen.add(olf_keywordFilterField); </presource> 3.89. Open Add background to the ''keywordFilterField'': <codesource lang="java">MyApp // Attempt to load a bitmap background try { Background bitmapBackground = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("background.png"), Background.POSITION_X_CENTER, Background.POSITION_Y_CENTER, Background.REPEAT_BOTH); _keywordFilterField.setBackground(bitmapBackground);   } catch (final IllegalArgumentException e) { UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { Dialog.alert(e.toString()); } }); }</codesource> class and add menu items inside the constructor. 3.9 10. Add view, add, edit and delete student options to the menu and set commands to executeinner class for custom search field: <presource lang="java"> MyScreen _mainScreen = new MyScreen final static class CustomKeywordField extends BasicEditField { // Constructor CustomKeywordField() { // Custom style. super(USE_ALL_WIDTH|NON_FOCUSABLE|NO_LEARNING|NO_NEWLINE); setLabel("Search: "); MenuItem view = new MenuItem}  /** * Intercepts ESCAPE key. * @see net.rim.device.api.ui.component.TextField#keyChar(char,int,int) */ protected boolean keyChar(char ch, int status, int time) { switch(ch) { case Characters.ESCAPE: // Clear keyword. if(super.getTextLength(new StringProvider) > 0) { setText("View Student"); return true; } } return super.keyChar(ch, 100status, 1time); view}   /** * Overriding super to add custom painting to our class. * @see net.rim.device.api.ui.setCommandField#paint(new CommandGraphics) */ protected void paint(Graphics graphics) { super.paint(graphics); // Draw caret. getFocusRect(new ViewCommandHandlerXYRect()); drawFocus(graphics, true); _mainScreen} } </source> 3.11.addMenuItemAdd the method that creates lists of students: <source lang="java"> private Vector getFromList() { Vector students = new Vector(view);
MenuItem adds = new MenuItemstudents.addElement(new StringProviderStudent("Add StudentAnastasia"), 200"Semionova", 2"asemionova1@learn.senecac.on.ca")); addsstudents.setCommandaddElement(new CommandStudent("Minoo", "Ziaei", "minoo.ziaei@senecac.on.ca")); students.addElement(new AddCommandHandlerStudent()"Ladan", "Zadeh", "lzahiroleslamzadeh@learn.senecac.on.ca")); _mainScreenstudents.addMenuItemaddElement(new Student(adds"Sergiu", "Ecob", "secob@learn.senecac.on.ca"));
MenuItem edit = new MenuItem(new StringProvider("Edit Student"), 300, 3)return students; edit.setCommand(new Command(new EditCommandHandler())); _mainScreen.addMenuItem(edit); MenuItem delete = new MenuItem(new StringProvider("Delete Student"), 400, 4); delete.setCommand(new Command(new DeleteCommandHandler())); _mainScreen.addMenuItem(delete); pushScreen(_mainScreen); </pre>3.10. Create inner classes to execute all commands: <pre> class ViewCommandHandler extends CommandHandler { public void execute(ReadOnlyCommandMetadata metadata, Object context){ Dialog.alert("View was selected"); }
}
  class AddCommandHandler extends CommandHandler/** * Adds a new element and updates the student list. * @param str The string to be added to the element list. */ { public void executeaddElementToList(ReadOnlyCommandMetadata metadata, Object contextStudent student){ Dialog _studentList.alertaddElement("Add was selected"student); }_keywordFilterField.updateList();
}
class EditCommandHandler extends CommandHandler </source> { public void execute(ReadOnlyCommandMetadata metadata, Object context){3.12. Add the method to access the <code>KeywordFilterField</code> object: Dialog.alert( <source lang="Edit was selectedjava"); } } class DeleteCommandHandler extends CommandHandler {> public void execute KeywordFilterField getKeywordFilterField(ReadOnlyCommandMetadata metadata, Object context){ Dialog.alert("Delete was selected") return _keywordFilterField; }
}
</presource> 3.1113. Now create Run the all other screen and implement menu commandsapplication:<br/>[[Image: BB_mainscreen.png | 300px]]<br/>3.14. Type the desired student name in the search field:<br/>[[Image: BB_search.png | 300px]]
1
edit

Navigation menu