1

I am using a ListField in BlackBerry and want to include a button with two text fields in the row like:

           Button
           Text1
           Text2  

But I am not able to add the buttons. All the help I've found is about adding images.

Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
Suman
  • 11
  • 2
  • 1
    I want to know exactly what do you want to do with buttons, row specific operations can be done easily without need of buttons. And if there are more operations on row of list field I use menu instead. – Prasham Nov 16 '10 at 06:14

3 Answers3

5

Take a look at How to customize list field in blackberry and Blackberry - how to add fields to listfield

Community
  • 1
  • 1
Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
0

by default ... list field provides the focus on a single row as a whole....and not to the single field on a row(as u told that u want to add three fields....buttons, textfield, textfield).

so i want to know why do u want to add buttons and two separate text-fields in a single row... I think its not easy if u want to get focus only on button OR only on a text-field....in a single row of a list field.

by the way... here is the sample code........ how u create three fields in a single row of list field...

just call the constructor of this list-field class in ur main screen's class and add it like.....

DetailListField _listField = new DetailListField();
add(_listField); 

DetailListField class -

class DetailListField extends ListField implements ListFieldCallback
{
    private Vector rows;
    private Font font;

    public DetailListField()
    {
        this(0, ListField.USE_ALL_WIDTH | DrawStyle.LEFT);
    }

    public DetailListField(int numRows, long style)
    {
        super(0, style);

        try
        {
            rows = new Vector();
            font = Font.getDefault().derive(Font.PLAIN, 7, Ui.UNITS_pt);

            setRowHeight(-2);                       
            setCallback(this);

            for (int x = 0 ; x < 5 ; x++)
            {
                TableRowManager row = new TableRowManager();

                // button, textfield, textfield
                ButtonField _btn = new ButtonField("Button", ButtonField.CONSUME_CLICK);
                _btn.setBorder(VISUAL_STATE_NORMAL, BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1),
                        new XYEdges(0x557788, 0xAA22BB, 0x557788, 0xAA22BB),
                        Border.STYLE_SOLID));
                row.add(_btn);

                BasicEditField _basicEdit1 = new BasicEditField(BasicEditField.EDITABLE | BasicEditField.FILTER_DEFAULT);
                _basicEdit1.setBorder(VISUAL_STATE_NORMAL, BorderFactory.createSimpleBorder(new XYEdges(2,2,2,2),
                        new XYEdges(0x557788, 0xAA22BB, 0x557788, 0xAA22BB),
                        Border.STYLE_SOLID));
                row.add(_basicEdit1);

                BasicEditField _basicEdit2 = new BasicEditField(BasicEditField.EDITABLE | BasicEditField.FILTER_DEFAULT);
                _basicEdit2.setBorder(VISUAL_STATE_NORMAL, BorderFactory.createSimpleBorder(new XYEdges(2,2,2,2),
                        new XYEdges(0x994422, 0xAA22BB, 0x994422, 0xAA22BB),
                        Border.STYLE_SOLID));
                row.add(_basicEdit2);


                // add id to the vector.
                rows.addElement(row); // returnData[x][0]);

                // call draw list row
                // then call constructor of manager class
            } 

            setSize(rows.size());
            invalidate();
        } catch(Exception e) {
        }
    }

    public void drawListRow(ListField list, Graphics g, int index, int y, int width)
    {
        try
        {
            DetailListField dl = (DetailListField)list;
            TableRowManager rowManager = (TableRowManager)dl.rows.elementAt(index);
            rowManager.drawRow(g, 0, y, width, list.getRowHeight());

        } catch(Exception e) {
        }
    }   

    protected boolean keyChar(char key, int status, int time)
    {   
        if (key == Characters.ENTER)
        {
            return true;
            // We've consumed the event.    
        }
        else if(key == Characters.ESCAPE)
        {
            return true;
        }             
        return super.keyChar(key, status, time);
    }

    protected boolean navigationClick(int status, int time)
    {
        try
        {
            // use below method if want to get label value from manager.
            final int index = this.getSelectedIndex();

            if(index >= 0) {
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        Dialog.alert("Selected index number : " + (index + 1));
                    }
                });
            }
        } catch (final Exception e) {
        }
        return true;   
    }

     public Object get(ListField listField, int index)
     {
         // TODO Auto-generated method stub
         return rows.elementAt(index);
     }

     public int getPreferredWidth(ListField listField)
     {
         // TODO Auto-generated method stub
         return 0;
     }

     public int indexOfList(ListField listField, String prefix, int start)
     {
         // TODO Auto-generated method stub
         return rows.indexOf(prefix, start);
     }


    /**
     *  MANAGER CLASS  
     */
    private class TableRowManager extends Manager
    {
        int _height = 0, _width = 0;
        int yPos = 0;

        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)
        {
            try
            {
                _height = height;
                _width = getPreferredWidth();

                yPos = y;

                // Arrange the cell fields within this row manager.
                // set the size and position of each field.
                layout(_width, _height);

                // Place this row manager within its enclosing list.
                setPosition(x, y);

                // Apply a translating/clipping transformation to the graphics
                // context so that this row paints in the right area.

                g.pushRegion(getExtent());
                //  Paint this manager's controlled fields.
                subpaint(g);
                g.setColor(0x00CACACA);
                g.drawLine(0, 0, getPreferredWidth(), 0);

                // Restore the graphics context.
                g.popContext();
            } catch(Exception e) {
                System.out.println("Exeception : (DetailListField) 4 : " + e.toString());
            }
        }

        // Arranges this manager's controlled fields from left to right within
        // the enclosing table's columns.
        protected void sublayout(int width, int height)
        {
            try
            {

                // set the bitmap field
                Field _field0 = getField(0);
                layoutChild(_field0, (_width/3) - 30 , _height - 20);
                setPositionChild(_field0, 2, 5);

                // set the name field
                Field _field1 = getField(1);
                _field1.setFont(font);
                layoutChild(_field1, (_width/3) - 30, _field1.getPreferredHeight());
                setPositionChild(_field1, (_width/3) - 30 + 10, 5);

                Field _field2 = getField(2);
                _field2.setFont(font);              
                layoutChild(_field2, (_width/3) - 30, _field2.getPreferredHeight());
                setPositionChild(_field2, ((_width/3) - 30)*2 + 20, 5);


                setExtent(_width, _height);

            } catch(Exception e) {
                System.out.println("Exeception : (DetailListField) 5 : " + e.toString());
            }
        }
        // The preferred width of a row is defined by the list renderer.
        public int getPreferredWidth()
        {
            return (Display.getWidth());
        }
        // The preferred height of a row is the "row height" as defined in the
        // enclosing list.
        public int getPreferredHeight()
        {
            return _height;
        }       
    }
}

bt still i dont know how to get focus on single field of a single row...

Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
0

usage:

ListCallBack _callBack = new ListCallBack();
_countries.setCallback(_callBack);

code:

private class ListCallBack implements ListFieldCallback{

    public void drawListRow(ListField listField, Graphics graphics,
                            int index, int y, int width) {
        for(int i = 0; i <= 23; i++) {
            graphics.drawBitmap(0, y, 48, 48, (Bitmap) MyApp._flagVector.elementAt(index), 0, 0);
        }

        String text = (String)MyApp._countryVector.elementAt(index);
        graphics.drawText(text, 65, y, 0, width);
    }

    public Object get(ListField listField, int index) {
        return MyApp._countryVector.elementAt(index);
    }

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

    public int indexOfList(ListField listField, String prefix, int start) {
        return MyApp._countryVector.indexOf(prefix, start);
    }
}
gevorg
  • 4,835
  • 4
  • 35
  • 52