Open main menu

CDOT Wiki β

Changes

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;
</presource3.3.Add three argument constructor: <source lang="java"> public Student(String firstName, String lastName, String email) { this.firstName = firstName; this.lastName = lastName; this.email = email; } </source> 3.4. Add an array in getter method for e-mail and override the <code>MyScreentoString()</code> from the Object class (it will be used to work with dataimplement <code>Comparator</code> interface): <source lang="java"> public String getEmail() { return email; } public String toString() { return firstName + " " + lastName; } </source> 3.5.Add class that will hold all Student objects and make necessary manipulations: <source lang="java"> public class StudentList extends SortedReadableList implements KeywordProvider {  public StudentList(Vector students) { super(new StudentListComparator()); loadFrom(students.elements()); }  /** * Adds a new element to the list. * @param element The element to be added. */ void addElement(Object element) { doAdd(element); }   /** * 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.Comparator#compare(Object, Object) */ public int compare(Object o1, Object o2) { if (o1 == null || o2 == null) { throw new IllegalArgumentException("Cannot compare null students"); } return o1.toString().compareTo(o2.toString()); } }  public String[] getKeywords(Object element) { if(element instanceof Student ) { return StringUtilities.stringToWords(element.toString()); } return null; } }<br/source> 3.46. The following elements are added to the <code>MyScreen()</code> constructor. Add a text field: <br/> <presource lang="java"> AutoTextEditField atf // A reference to the UiApplication instance for use in this class _app = app; // Obtain a reference to the UiApplication's KeywordFilterField _keywordFilterField = new AutoTextEditField_app.getKeywordFilterField("Student Name);</source> 3.7. Other elements are added inside the <code>ViewStudentApp</code> class. Add necessary private fields: <source lang=", java"")> private KeywordFilterField _keywordFilterField; private StudentList _studentList; private Vector _students; </presource> 3.58. Set the border Add search field and background for the text fieldstudents list: <presource lang="java"> MyScreen _mainScreen = new MyScreen(this);  _students = getFromList(); _studentList = new StudentList(_students);  Border roundedBorder // Add our list to a KeywordFilterField object. _keywordFilterField = BorderFactorynew KeywordFilterField(); _keywordFilterField.createRoundedBorder(new XYEdgessetSourceList(5_studentList, 5, 5, 5_studentList), Border;    _mainScreen.setTitle(_keywordFilterField.STYLE_SOLIDgetKeywordField()); _mainScreen.add(_keywordFilterField); </source> 3.9. Add background to the ''keywordFilterField'': <source lang="java"> // Attempt to load a bitmap background try { Background solidBackground bitmapBackground = BackgroundFactory.createSolidBackgroundcreateBitmapBackground(Bitmap.getBitmapResource(Color"background.png"), Background.POSITION_X_CENTER, Background.POSITION_Y_CENTER, Background.LIGHTSTEELBLUEREPEAT_BOTH); atf _keywordFilterField.setBordersetBackground(roundedBorderbitmapBackground);   } catch (final IllegalArgumentException e) { UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { atf Dialog.alert(e.setBackgroundtoString(solidBackground)); add(atf } }); }</presource> 3.610. Add a inner class for custom search buttonfield: <presource lang="java"> ButtonField _bf=new ButtonField final static class CustomKeywordField extends BasicEditField { // Constructor CustomKeywordField() { // Custom style. super(USE_ALL_WIDTH|NON_FOCUSABLE|NO_LEARNING|NO_NEWLINE); setLabel("Search: "); }  /** * 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() > 0) { setText(""); return true; } } return super.keyChar(ch, status, time); }   /** * Overriding super to addcustom painting to our class. * @see net.rim.device.api.ui.Field#paint(Graphics) */ protected void paint(_bfGraphics graphics) { super.paint(graphics); // Draw caret. getFocusRect(new XYRect()); drawFocus(graphics, true); } } </presource> 3.711. Add the array method that creates lists of students to the list field: <presource lang="java"> ObjectListField olf private Vector getFromList() { Vector students = new ObjectListFieldVector(); for students.addElement(new Student(int i=0"Anastasia", "Semionova", "asemionova1@learn.senecac.on.ca")); i< ls students.addElement(new Student("Minoo", "Ziaei", "minoo.ziaei@senecac.on.lengthca")); i++ students.addElement(new Student("Ladan", "Zadeh", "lzahiroleslamzadeh@learn.senecac.on.ca")) {; olf students.insertaddElement(inew Student("Sergiu", ls[i]"Ecob", "secob@learn.senecac.on.ca")); return students; }  /** * Adds a new element and updates the student list. * @param str The string to be added to the element list.getFirstName */ void addElementToList(Student student) + " " + ls[i]{ _studentList.getLastNameaddElement(student); _keywordFilterField.updateList(); } add </source> 3.12. Add the method to access the <code>KeywordFilterField</code> object: <source lang="java"> KeywordFilterField getKeywordFilterField(olf){ return _keywordFilterField; } </presource3.13. Run the application:<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