2012-05-07 51 views
1

我是黑莓開發的新手。當我點擊任何列表項目時,我想添加一個複選框或刻度線標記。當我嘗試運行這是給出了一個104錯誤。如何添加一個複選框點擊列表字段

public final class ListDemoScreen extends MainScreen { 
    private Vector _listElements; 

    private int LEFT_OFFSET = 10; 
    private int TOP_OFFSET = 10; 
    ListField list; 
    private ListField _checkList; 
    private MenuItem _toggleItem; 

    public ListDemoScreen() { 
     super(Manager.NO_VERTICAL_SCROLL); 
     // Set the displayed title of the screen 
     setTitle("List Demo 1"); 
     add(new LabelField("Fruits List", LabelField.FIELD_HCENTER)); 
     add(new SeparatorField()); 

     _listElements = new Vector(); 
     add(new SeparatorField()); 
     list = new ListField(); 
     ListCallback _callback = new ListCallback(this); 

     list.setCallback(_callback); 
     list.setSize(4); 
     int index = list.getSelectedIndex(); 

     add(list); 

     createField(); 

    } 

    protected void createField() { 
     String itemOne = "Apple"; 
     String itemTwo = "Blackberry"; 
     String itemthree = "Grapes"; 
     String itemfour = "Banana"; 
     _listElements.addElement(itemOne); 

     _listElements.addElement(itemTwo); 
     _listElements.addElement(itemthree); 
     _listElements.addElement(itemfour); 
     reloadList(); 

    } 

    private void reloadList() { 
     list.setSize(_listElements.size()); 
    } 


    public boolean invokeAction(int action) { 
     switch (action) { 
     case ACTION_INVOKE: // Trackball click. 
      int index = list.getSelectedIndex(); 
      ChecklistData data = (ChecklistData) _listElements.elementAt(index); 
      data.toggleChecked(); 
      _listElements.setElementAt(data, index); 
      list.invalidate(index); 
      return true; // We've consumed the event. 
     } 
     return super.invokeAction(action); 

    } 


    class ListCallback implements ListFieldCallback { 
     ListDemoScreen listDemoScreen; 

     public ListCallback(ListDemoScreen listDemoScreen) 
     { 
      this.listDemoScreen=listDemoScreen; 

     } 

     public void drawListRow(ListField list, Graphics g, int index, int y, 
       int w) { 

      String text = (String) _listElements.elementAt(index); 
      g.drawText(text, 60, y + 5, 0, w); 
      text = (String) _listElements.elementAt(index); 
      Bitmap bitm = Bitmap.getBitmapResource("bullet_arrow.png"); 
      int xpos = LEFT_OFFSET; 
      int ypos = TOP_OFFSET + y; 
      w = bitm.getWidth(); 
      int h = bitm.getHeight(); 

      g.drawBitmap(xpos, ypos, w, h, bitm, 0, 0); 

      xpos = w + 20; 

      ChecklistData currentRow = (ChecklistData)this.get(list, index); 

      StringBuffer rowString = new StringBuffer(); 

      // If it is checked draw the String prefixed with a checked box, 
      // prefix an unchecked box if it is not. 
      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. 
      g.drawText(rowString.toString(), 0, y, 0, w); 

     } 

     public Object get(ListField list, int index) { 
      return _listElements.elementAt(index); 
     } 

     public int indexOfList(ListField list, String prefix, int string) { 
      return _listElements.indexOf(prefix, string); 
     } 

     public int getPreferredWidth(ListField list) { 
      return Display.getWidth(); 
     } 

    } 

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

     /* 
     * ChecklistData() { _stringVal = ""; _checked = false; } 
     */ 

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

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

     private boolean isChecked() { 
      return _checked; 
     } 

     /* 
     * private void setStringVal(String stringVal) { _stringVal = stringVal; 
     * } 
     * 
     * private void setChecked(boolean checked) { _checked = checked; } 
     */ 

     // Toggle the checked status. 
     private void toggleChecked() { 
      _checked = !_checked; 
     } 
    } 

    /* (non-Javadoc) 
    * @see net.rim.device.api.ui.container.MainScreen#makeMenu(net.rim.device.api.ui.component.Menu, int) 
    */ 
    protected void makeMenu(Menu menu, int instance) { 

     // TODO Auto-generated method stub 
     Field focus = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus(); 
     if(focus == _checkList) 
     { 
      //The _checkList ListField instance has focus. 
      //Add the _toggleItem MenuItem. 
      menu.add(_toggleItem); 
     } 


     super.makeMenu(menu, instance); 
    } 
} 
+0

引用此鏈接將複選框添加到列表字段。 http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800345/How_To_-_Create_a_ListField_with_check_boxes.html?nodeid=1165752&vernum=0 – Signare

+0

您能否花些時間寫下您的問題?它應該包含一個[**簡短的**,自包含的,正確的示例](http://sscce.org/);在嘗試解決問題時,請清楚問題的描述和[您嘗試過的內容](http://mattgemmell.com/2008/12/08/what-have-you-tried/)的描述。 – Ben

回答

0

你可以很容易地找到了ClassCastException,如果你一直在小心翼翼地調試你的代碼。你在你的createField()方法添加在_listElementsVectorString對象,並試圖將String對象投射到一個ChecklistData對象在invokeAction(int action)方法:

ChecklistData data = (ChecklistData) _listElements.elementAt(index); 

你應該在_listElements向量,之後插入ChecklistData對象你顯然不能投那些元素融入String S IN的drawListRow()方法:

String text = (String) _listElements.elementAt(index); 

固定代碼,請參見下面的(不知道你想與bullet_arrow.png做什麼......所以,我註釋掉了部分修改後位):

import java.util.Vector; 
import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.system.Characters; 
import net.rim.device.api.system.Display; 
import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.Manager; 
import net.rim.device.api.ui.MenuItem; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.component.ListField; 
import net.rim.device.api.ui.component.ListFieldCallback; 
import net.rim.device.api.ui.component.Menu; 
import net.rim.device.api.ui.component.SeparatorField; 
import net.rim.device.api.ui.container.MainScreen; 

public final class ListDemoScreen extends MainScreen { 

    private Vector _listElements; 

    ListField list; 
    private ListField _checkList; 
    private MenuItem _toggleItem; 

    public ListDemoScreen() { 
     super(Manager.NO_VERTICAL_SCROLL); 
     // Set the displayed title of the screen 
     setTitle("List Demo 1"); 
     add(new LabelField("Fruits List", LabelField.FIELD_HCENTER)); 
     add(new SeparatorField()); 

     _listElements = new Vector(); 
     add(new SeparatorField()); 
     list = new ListField(); 
     ListCallback _callback = new ListCallback(this); 

     list.setCallback(_callback); 
     list.setSize(4); 
     int index = list.getSelectedIndex(); 

     add(list); 

     createField(); 

    } 

    protected void createField() { 
//  String itemOne = "Apple"; 
//  String itemTwo = "Blackberry"; 
//  String itemthree = "Grapes"; 
//  String itemfour = "Banana"; 
     ChecklistData itemOneCheckList = new ChecklistData("Apple", false); 
     ChecklistData itemTwoCheckList = new ChecklistData("Blackberry", false); 
     ChecklistData itemThreeCheckList = new ChecklistData("Grapes", false); 
     ChecklistData itemFourCheckList = new ChecklistData("Banana", false); 

     _listElements.addElement(itemOneCheckList); 
     _listElements.addElement(itemTwoCheckList); 
     _listElements.addElement(itemThreeCheckList); 
     _listElements.addElement(itemFourCheckList); 
     reloadList(); 

    } 

    private void reloadList() { 
     list.setSize(_listElements.size()); 
    } 

    public boolean invokeAction(int action) { 
     switch (action) { 
     case ACTION_INVOKE: // Trackball click. 
      int index = list.getSelectedIndex(); 
      ChecklistData data = (ChecklistData) _listElements.elementAt(index); 
      data.toggleChecked(); 
      _listElements.setElementAt(data, index); 
      list.invalidate(index); 
      return true; // We've consumed the event. 
     } 
     return super.invokeAction(action); 

    } 

    class ListCallback implements ListFieldCallback { 
     ListDemoScreen listDemoScreen; 

     public ListCallback(ListDemoScreen listDemoScreen) { 
      this.listDemoScreen = listDemoScreen; 

     } 

     public void drawListRow(ListField list, Graphics g, int index, int y, 
       int w) { 

      ChecklistData checkListData = (ChecklistData) _listElements.elementAt(index); 
      String text = checkListData.getStringVal(); 
//   g.drawText(text, 60, y + 5, 0, w); 
//   Bitmap bitm = null; 
//   if(checkListData.isChecked()) { 
//    bitm = Bitmap.getBitmapResource("bullet_arrow1.png"); 
//   } else { 
//    bitm = Bitmap.getBitmapResource("bullet_arrow2.png"); 
//   } 

//   w = bitm.getWidth(); 
//   int h = bitm.getHeight(); 
//   
//   int xpos = 2; 
//   int heightDifference = (list.getRowHeight(index) - h); 
//   int ypos = y + (heightDifference > -1 ? heightDifference : 0)/2; 
// 
//   g.drawBitmap(xpos, ypos, w, h, bitm, 0, 0); 

//   xpos = w + 20; 

      ChecklistData currentRow = (ChecklistData) this.get(list, index); 

      StringBuffer rowString = new StringBuffer(); 

      // If it is checked draw the String prefixed with a checked box, 
      // prefix an unchecked box if it is not. 
      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. 
      g.drawText(rowString.toString(), 0, y, 0, -1); 

     } 

     public Object get(ListField list, int index) { 
      return _listElements.elementAt(index); 
     } 

     public int indexOfList(ListField list, String prefix, int string) { 
      return _listElements.indexOf(prefix, string); 
     } 

     public int getPreferredWidth(ListField list) { 
      return Display.getWidth(); 
     } 

    } 

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

     /* 
     * ChecklistData() { _stringVal = ""; _checked = false; } 
     */ 

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

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

     private boolean isChecked() { 
      return _checked; 
     } 


     // Toggle the checked status. 
     private void toggleChecked() { 
      _checked = !_checked; 
     } 

    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see 
    * net.rim.device.api.ui.container.MainScreen#makeMenu(net.rim.device.api 
    * .ui.component.Menu, int) 
    */ 
    protected void makeMenu(Menu menu, int instance) { 

     // TODO Auto-generated method stub 
     Field focus = UiApplication.getUiApplication().getActiveScreen() 
       .getLeafFieldWithFocus(); 
     if (focus == _checkList) { 
      // The _checkList ListField instance has focus. 
      // Add the _toggleItem MenuItem. 
      menu.add(_toggleItem); 
     } 

     super.makeMenu(menu, instance); 
    } 

} 

希望這會有所幫助。

+0

謝謝你!有效。 「bullet_arrow.png」只是一張圖片。我試圖實現的是「圖像文本複選框」,當我點擊列表項時,應該檢查它。和蜱盒不應該是可見的。只有我想要的刻度。 –

+0

我想用圖像和刻度標記來完成檢查列表。像「img」_listElement tickmark。並且複選框框應該是不可見的,只有刻度線可以看到。 –

+0

鑑於更新的代碼[這裏](http://stackoverflow.com/questions/10517065/checkbox-on-right-side-of-text-in-listfield/10522361#10522361) –

相關問題