2011-11-10 12 views
2

もう一度チェックボックスを使ってリストビューを実装することに固執しました。ブラックベリーの連絡先をチェックボックス付きリストビューに表示する必要があります。私はコードを実行しているときに余分なメモリを消費しています。私はそれが何も表示されていないコードを投稿しています。私はどこで間違いを犯しているのか、何を修正するのか教えてください。Blackberry:チェックボックスとリストビュー

更新日: コードは現在実行中ですが、スクロール中に配列外の例外をスローしています。あなたはどこに問題があるのか​​教えていただけますか?

public class CheckboxListField extends MainScreen implements ListFieldCallback { 
     private Vector _listData = new Vector(); 
     private ListField listField; 
     private ContactList blackBerryContactList; 
     private BlackBerryContact blackBerryContact; 
     private Vector blackBerryContacts; 

     private class ChecklistData 
     { 
      private String _stringVal; 
      private boolean _checked; 

      ChecklistData(String stringVal, boolean checked) 
      { 
       _stringVal = stringVal; 
       _checked = checked; 
      } 

      //Get/set methods. 
      private String getStringVal() 
      { 
       return _stringVal; 
      } 

      private boolean isChecked() 
      { 
       return _checked; 
      } 
     } 

     CheckboxListField() 
     {  
      listField = new ListField(); 
      listField.setCallback(this); 
      reloadContactList(); 

      for(int count = 0; count < blackBerryContacts.size(); ++count) 
      { 
       BlackBerryContact item = 
        (BlackBerryContact)blackBerryContacts.elementAt(count); 
        String displayName = getDisplayName(item); 
        add(new RichTextField("Check1")); 
        _listData.addElement(new ChecklistData(displayName, false)); 
        add(new RichTextField("Check2")); 
        listField.insert(count); 
        add(new RichTextField("Check3")); 
        add(new RichTextField(blackBerryContacts.size())); 
        add(new RichTextField(displayName)); 
       } 
      add(listField); 
     } 

     private boolean reloadContactList() 
     { 
      try { 
       blackBerryContactList = 
        (ContactList)PIM.getInstance().openPIMList 
        (PIM.CONTACT_LIST, PIM.READ_ONLY); 

       Enumeration allContacts = blackBerryContactList.items(); 
       blackBerryContacts = enumToVector(allContacts); 
       listField.setSize(blackBerryContacts.size()); 
       return true; 
      } catch (PIMException e) 
      { 
       return false; 
      } 
     } 

     //Convert the list of contacts from an Enumeration to a Vector 
     private Vector enumToVector(Enumeration contactEnum) { 
      Vector v = new Vector(); 

      if (contactEnum == null) 
       return v; 

      while (contactEnum.hasMoreElements()) { 
       v.addElement(contactEnum.nextElement()); 
      } 

      return v; 
      } 

     public void drawListRow(ListField list, Graphics graphics, int index, int y, int w) 
     { 
      ChecklistData currentRow = (ChecklistData)this.get(list, index); 
      StringBuffer rowString = new StringBuffer(); 

      if (currentRow.isChecked()) 
      { 
       rowString.append(Characters.BALLOT_BOX_WITH_CHECK); 
      } 
      else 
      { 
       rowString.append(Characters.BALLOT_BOX); 
      } 

      //Append a couple spaces and the row's text. 
      rowString.append(Characters.SPACE); 
      rowString.append(Characters.SPACE); 
      rowString.append(currentRow.getStringVal()); 

      //Draw the text. 
      graphics.drawText(rowString.toString(), 0, y, 0, w); 
     } 

     public static String getDisplayName(Contact contact) 
      { 
       if (contact == null) 
       { 
        return null;  
       } 

       String displayName = null; 

       // First, see if there is a meaningful name set for the contact. 
       if (contact.countValues(Contact.NAME) > 0) { 
        final String[] name = contact.getStringArray(Contact.NAME, 0); 
        final String firstName = name[Contact.NAME_GIVEN]; 
        final String lastName = name[Contact.NAME_FAMILY]; 
        if (firstName != null && lastName != null) { 
         displayName = firstName + " " + lastName; 
        } else if (firstName != null) { 
         displayName = firstName; 
        } else if (lastName != null) { 
         displayName = lastName; 
        } 

        if (displayName != null) { 
         final String namePrefix = name[Contact.NAME_PREFIX]; 
         if (namePrefix != null) { 
          displayName = namePrefix + " " + displayName; 
         } 
         return displayName; 
        } 
       } 
       return displayName; 
      } 

     //Returns the object at the specified index. 
     public Object get(ListField list, int index) 
     { 
      return _listData.elementAt(index); 
     } 

     //Returns the first occurence of the given String, bbeginning the search at index, 
     //and testing for equality using the equals method. 
     public int indexOfList(ListField list, String p, int s) 
     { 
      return -1; 
     } 

     //Returns the screen width so the list uses the entire screen width. 
     public int getPreferredWidth(ListField list) 
     { 
      return Display.getWidth(); 
     } 
    } 

答えて

1

urカスタマイズリストを作成した後。 VerticalFieldManagerの内部に追加します。この縦型FieldManagerを画面に追加します。 ここにいくつかのコード...リストを追加する画面にこのコードを書く

int DISPLAY_WIDTH = Display.getWidth(); 
    int DISPLAY_HEIGHT = Display.getHeight(); 
    VerticalFieldManager VFM mListManager = new SizedVFM(DISPLAY_WIDTH, DISPLAY_HEIGHT - 40); 
    mListManager.add(mListField); 
    add(mListManager);  
関連する問題