Checkbox in CustomListField in Blackberry

772 views Asked by At

I am creating a CustomList. In each row I am having a image, some text fields and a checkbox. But i am not able to check and uncheck the checkbox. I have gone through this link but cant solve my problem.

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

this will add a menu item in the menu to change the checkbox status. I want the normal checkbox behaviour. Please help

this is my code

class CustomList extends ListField implements ListFieldCallback
{

public Vector rows;
private Bitmap p1;
int z = this.getRowHeight();    
LabelField task,task2,task3;
CheckboxField checkbox[]= new CheckboxField[40];
int rowcount;
public CustomList(int rowcount,String text1,double text2,String type,Bitmap p)
{
    super(0, ListField.MULTI_SELECT);
    this.rowcount= rowcount;
    setRowHeight(z*3);
    setEmptyString("Hooray, no tasks here!", DrawStyle.HCENTER);
    setCallback(this);        
    p1 = p;     
    rows = new Vector();

    for (int x = 0; x < rowcount; x++) 
    {
        checkbox[x]=new CheckboxField("",false,Field.USE_ALL_HEIGHT|Field.EDITABLE|Field.FIELD_VCENTER|Field.FOCUSABLE);
    }
    for (int x = 0; x < rowcount; x++) 
    {
        TableRowManager row = new TableRowManager();            
        row.add(checkbox[x]);
        row.add(new BitmapField(p1));
        task= new LabelField("abc");
        row.add(task);
        task2=new LabelField("abc2");
        row.add(task2);
        task3= new LabelField("abc3");
        row.add(task3);
        rows.addElement(row);
    }
    setSize(rows.size());
}
public void drawListRow(ListField listField, Graphics g, int index, int y,int width) 
{
    CustomList list = (CustomList) listField;
    TableRowManager rowManager = (TableRowManager) list.rows.elementAt(index);
    rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}

private class TableRowManager extends Manager 
{
    public TableRowManager() 
    {
    super(0);
    }

// Causes the fields within this row manager to be layed out then
// painted.
    public void drawRow(Graphics g, int x, int y, int width, int height) 
    {
        layout(width, height);
        setPosition(x, y);
        g.pushRegion(getExtent());
        subpaint(g);
        g.setColor(0x00CACACA);
        g.popContext();
    }
    protected void sublayout(int width, int height) 
    {
        // set the size and position of each field.
    }
    public int getPreferredWidth() 
    {
        return UIConstants.SCREEN_WIDTH;
    }
    public int getPreferredHeight() 
    {
        return getRowHeight();
    }
}
}

I am trying to implement listfield with checkboxes and i have seen its code in the knowledge base.

But i want that when i click on the checkbox it should be checked or unchecked onlyand when i click on the rest part of the row, i.e. anywhere except the checkbox then nothing should occur without checking the checkbox

How can i do that ?? Or i should do something else to do it.. Please suggest..

2

There are 2 answers

4
Rince Thomas On
  boolean checked = false;
  Vector box1 = new Vector();  
  for(int i=0;i<vector.size();i++){
    FriendsRequestObject co_vec = (FriendsRequestObject)vector.elementAt(i);
    String name=co_vec.getSender_name();
    CheckboxField box = new CheckboxField(" "+name , checked, Field.USE_ALL_WIDTH){
        public void paint(Graphics graphics) {
               graphics.setColor(Color.WHITE);
              super.paint(graphics);
            }
         };
         box1.addElement(box);
         box.setMargin(8, 0, 0, 5);
         add(box);

      }

then add a button. On the click listener of the button,

         for(int d=0; d<box1.size(); d++){
            CheckboxField box=(CheckboxField)box1.elementAt(d);
                  if(box.getChecked()==true){
                    FriendsRequestObject co_vec = (FriendsRequestObject)vector.elementAt(d);
                    String name_ = co_vec.getSender_name();
                    Dialog.alert(name_);// here you can implement your code
                  }
                }
2
BBdev On

Do some thing like this where you are overriding your sublayOut method() in table row manager class

protected void sublayout(int width, int height) 
{
    Field field = getField(0);            
                layoutChild(field, Display.getWidth(),   Display.getHeight());             
                setPositionChild(field, 10, 20);

Field field = getField(0);            
                layoutChild(field, Display.getWidth(),    Display.getHeight());            
                setPositionChild(field, 10, 20);

                //Set field           
                field = getField(1);
                layoutChild(field, 120, fontHeight - 1 );             
                setPositionChild(field, 70, 05);

                // set the list field             
                field = getField(2);              
                layoutChild(field, 150, fontHeight +10);              
                setPositionChild(field, 70, 25);

                // set the field               
                field = getField(3);              
                layoutChild(field, (Display.getWidth()/6)-12, fontHeight + 10);       
                setPositionChild(field, 70, 45);    
                setExtent(preferredWidth, getPreferredHeight());
}

change the padding as your require ments.

And you can also check this link create a listField with check box. Thanks