Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
Himanshu_berry
Posts: 138
Registered: ‎04-19-2012
My Device: No

can we add many Fields in ListField

Hello all,

 

             I want to know that can i add 3 or 4 Fields Like image,textfield etc in a single row of listfield.

 

 

Thanks

Please use plain text.
Developer
tklanilkumar
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800

Re: can we add many Fields in ListField

yes, you can achevice it by take a manager add fields to that.now you can add the manager to listfied
*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Please use plain text.
Developer
simon_hain
Posts: 16,137
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: can we add many Fields in ListField

no, a listfield is not a manager, you cannot add fields to a listfield.
what a listfield displays is controlled by drawListRow.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Trusted Contributor
Himanshu_berry
Posts: 138
Registered: ‎04-19-2012
My Device: No

Re: can we add many Fields in ListField

Hello simon ,

            Can u guide me that how can i create below screen. it is a ListField

            I tried alot but no solution found...Display data is coming from Web service.

 

9810.jpg.

                                  

Please use plain text.
Developer
simon_hain
Posts: 16,137
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: can we add many Fields in ListField

check this sample:
http://supportforums.blackberry.com/t5/Java-Development/Create-a-ListField-with-check-boxes/ta-p/443...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Trusted Contributor
Himanshu_berry
Posts: 138
Registered: ‎04-19-2012
My Device: No

Re: can we add many Fields in ListField

Thanks for reply Mr.simon
how can i add more items 
like checkbox firstelement second third in one row. 

package com.samples.checkboxListField;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import java.util.Vector;

class CheckboxListField extends UiApplication implements ListFieldCallback
{
    private static final String[] _elements = {"First element", "Second element", "Third element", "Fourth element", "Fifth element"};
    private Vector _listData = new Vector();
    private ListField _checkList;
    private MenuItem _toggleItem;
    
    //A class to hold the Strings in the CheckBox and it's checkbox state (checked or unchecked).
    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;
        }
    }
    
    public static void main(String args[])
    {
        CheckboxListField theApp = new CheckboxListField();
        theApp.enterEventDispatcher();
    }
    
    CheckboxListField() 
    {    
    
        //The menu item added to the screen when the _checkList field has focus.
        //This menu item toggles the checked/unchecked status of the selected row.
        _toggleItem = new MenuItem("Change Option", 200, 10) 
        {
            public void run()
            {
                //Get the index of the selected row.
                int index = _checkList.getSelectedIndex();
                
                //Get the ChecklistData for this row.
                ChecklistData data = (ChecklistData)_listData.elementAt(index);
                
                //Toggle its status.
                data.toggleChecked();
                
                //Update the Vector with the new ChecklistData.
                _listData.setElementAt(data, index);
                
                //Invalidate the modified row of the ListField.
                _checkList.invalidate(index);
            }
        };    
    
        MainScreen myScreen = new MainScreen()
        {
            //Override the makeMenu method so we can add a custom menu item
            //if the checkbox ListField has focus.
            protected void makeMenu(Menu menu, int instance)
            {
                Field focus = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();
                if(focus == _checkList) 
                {
                    //The _checkList ListField instance has focus.
                    //Add the _toggleItem MenuItem.
                    menu.add(_toggleItem);
                }
                
                //Create the default menu.
                super.makeMenu(menu, instance);
            }            
        };
        
        //Add a title to the screen.
        myScreen.setTitle(new LabelField("Checkbox ListField sample"));

        _checkList = new ListField()
        {
            //Allow the space bar to toggle the status of the selected row.
            protected boolean keyChar(char key, int status, int time)
            {
                boolean retVal = false;
                
                //If the spacebar was pressed...
                if (key == Characters.SPACE)
                {
                    //Get the index of the selected row.
                    int index = getSelectedIndex();
                    
                    //Get the ChecklistData for this row.
                    ChecklistData data = (ChecklistData)_listData.elementAt(index);
                    
                    //Toggle its status.
                    data.toggleChecked();
                    
                    //Update the Vector with the new ChecklistData.
                    _listData.setElementAt(data, index);
                    
                    //Invalidate the modified row of the ListField.
                    invalidate(index);
                    
                    //Consume this keyChar (key pressed).
                    retVal = true;
                }
                return retVal;
            }
        };
        
        //Set the ListFieldCallback
        _checkList.setCallback(this);
        
        int elementLength = _elements.length;
        
        //Populate the ListField & Vector with data.
        for(int count = 0; count < elementLength; ++count)
        {
            //Even rows will be checked, odd will be unchecked.
            if (count % 2 == 0)
            {
                _listData.addElement(new ChecklistData(_elements[count], true));
            }
            else
            {
                _listData.addElement(new ChecklistData(_elements[count], false));
            }
            _checkList.insert(count);
        }

        //Add the ListField to the screen.
        myScreen.add(_checkList);
        
        //Push the screen onto the display stack.
        pushScreen(myScreen);
    
    }
    
    //ListFieldCallback impelmentation.
    
    //Draws the list row.
    public void drawListRow(ListField list, Graphics graphics, int index, int y, int w) 
    {
        //Get the ChecklistData for the current row.
        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.
        graphics.drawText(rowString.toString(), 0, y, 0, w);
    }
    
    //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 listElements.getSelectedIndex();
        return _listData.indexOf(p, s);
    }
    
    //Returns the screen width so the list uses the entire screen width.
    public int getPreferredWidth(ListField list) 
    {
        return Graphics.getScreenWidth();
    }
}  

 

Please use plain text.
Developer
simon_hain
Posts: 16,137
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: can we add many Fields in ListField

try to understand what the code does.

you do NOT add checkboxes to the listfield. the listfield only draws the graphics of a checkbox, either checked or unchecked.

you could draw as many checkboxes as you want, but the logic to check/uncheck them would be complicated as you cannot focus them, only the full row can be focused.

if you absolutely need multiple checkboxes listfield is not a good approach, use a manager instead.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
rabiray
Posts: 141
Registered: ‎07-13-2011
My Device: BB Bold

Re: can we add many Fields in ListField

There is nice tutorial as well on listfield

Have a look

 

http://developerlife.com/tutorials/?p=898

Please use plain text.
Trusted Contributor
Himanshu_berry
Posts: 138
Registered: ‎04-19-2012
My Device: No

Re: can we add many Fields in ListField

Rabi Can u see my image.

Please use plain text.
Developer
rabiray
Posts: 141
Registered: ‎07-13-2011
My Device: BB Bold

Re: can we add many Fields in ListField

Yes I can...As simon advised..

 

Do not use a image for check box but add aCheckBoxField to ListField.

 

This will allow you to focus and click on it.

Please use plain text.