2012-05-07 4 views
1

검은 딸기 개발을 처음 사용했습니다. 목록 항목을 클릭 할 때 체크 박스 나 눈금 표시를 추가하고 싶습니다. 이것을 실행하려고하면 104 오류가 발생합니다. 주의 깊게 코드를 디버깅 한 경우가있을 수listfield에서 클릭하면 확인란을 추가하는 방법

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/)가 포함되어야합니다. 문제의 원인에 대한 _clear_ 설명과 문제 해결을 시도 할 때 [시도한 내용] (http://mattgemmell.com/2008/12/08/what-have-you-tried/)에 대한 설명. – Ben

답변

0

쉽게 ClassCastException을 발견했다. 당신은 당신의 createField() 방법에 _listElementsVector에서 String 객체를 추가하고, invokeAction(int action) 방법에 ChecklistData 객체로 String 개체를 캐스팅하려고 : 당신은 _listElements 벡터와 그 이후 ChecklistData 개체를 삽입해야

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

당신은 분명히 drawListRow() 방법에 String들로 이러한 요소를 캐스팅 할 수 없습니다

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 눈금처럼. 확인란이 눈에 보이지 않아야합니다. –

+0

업데이트 된 코드 [here] (http://stackoverflow.com/questions/10517065/checkbox-on-right-side-of-text-in-listfield/10522361#10522361) –

관련 문제