Search code examples
blackberrylistfield

In Listfield not able to view the fields in blackberry


i have tried this link How to customize a ListField in BlackBerry? to create the ListField with Three lablefields for every list row.but iam not able see the list field items.but i can say that listfield is added.because on navigation click i am able get the selected index of the listfield.please anybody help where iam doing mistake.The code i have tried is...

public class InboxWithOutCheckboxeslistfield extends ListField implements ListFieldCallback  {
    private Vector rows;
    private TableRowManager row ;
    private LabelField UserName,Message,Timestamp;


    /**
     * Creates a new MyScreen object
     */
     public InboxWithOutCheckboxeslistfield() {
        // TODO Auto-generated constructor stub

        super(0, ListField.MULTI_SELECT);
        setRowHeight(80);
        setCallback(this);
        rows = new Vector();
        for (int x = 0; x < 10; x++) {
            row = new TableRowManager();
            UserName = new LabelField("" + String.valueOf(x),
                    DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
                    | DrawStyle.LEFT);
            UserName.setText("name");
            row.add(UserName);
            Message = new LabelField("" + String.valueOf(x),
                    DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
                    | DrawStyle.RIGHT);
            Message.setText("hai");
            row.add(Message);
            Timestamp = new LabelField("" + String.valueOf(x),
                    DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
                    | DrawStyle.RIGHT);
            Timestamp.setText("hai");
            row.add(Timestamp);
            rows.addElement(row);

        }
        setSize(rows.size());


    }

    public void drawListRow(ListField listField, Graphics graphics, int index,
            int y, int width) {
        // TODO Auto-generated method stub

        InboxWithOutCheckboxeslistfield list = (InboxWithOutCheckboxeslistfield) listField;


        TableRowManager rowManager = (TableRowManager) list.rows.elementAt(index);
        rowManager.drawRow(graphics, 0, y, width, list.getRowHeight());

    }

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

    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 0;
    }


    private class TableRowManager extends Manager {

        protected TableRowManager(){super(0);
        // TODO Auto-generated constructor stub}
        }

        public void drawRow(Graphics graphics, int i, int y, int width,
                int rowHeight) {
            // TODO Auto-generated method stub
            // Arrange the cell fields within this row manager.
            layout(width, rowHeight);

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

            // Apply a translating/clipping transformation to the graphics
            // context so that this row paints in the right area.
            graphics.pushRegion(getExtent());

            // Paint this manager's controlled fields.
            subpaint(graphics);

            graphics.setColor(0x00CACACA);
            graphics.drawLine(0, 0, getPreferredWidth(), 0);

            // Restore the graphics context.
            graphics.popContext();

        }

        protected void sublayout(int width, int height) {
            // TODO Auto-generated method stub
            // set the size and position of each field.
            int fontHeight = Font.getDefault().getHeight();
            int preferredWidth = getPreferredWidth();

            Field field = getField(0);

            layoutChild(field, preferredWidth - 16, fontHeight + 1);
            setPositionChild(field, 34, 3);
            // set the list name label field
            field = getField(1);
            layoutChild(field, 150, fontHeight + 1);
            setPositionChild(field, 34, fontHeight + 6);

            // set the due time name label field
            field = getField(2);
            layoutChild(field, 150, fontHeight + 1);
            setPositionChild(field, preferredWidth - 152, fontHeight + 6);

            setExtent(getPreferredWidth(), getPreferredHeight());

        }
        // The preferred width of a row is defined by the list renderer.  
        public int getPreferredWidth()  
        {       

            return getWidth();        
        } 
        // The preferred height of a row is the "row height" as defined in the  
        // enclosing list.      
        public int getPreferredHeight()     
        { 
            return getHeight();       
        } 

    }
    protected boolean navigationClick(int status, int time) {
        int index1 = getSelectedIndex();
        System.out.println("The Clikced item is:"+index1);
        return true;
    }
}

and the mainscreen class is

public class InboxWithOutCheckboxes extends MainScreen{

    private InboxWithOutCheckboxeslistfield inboxWithOutCheckboxeslistfield;

    public InboxWithOutCheckboxes(){
        super();
        inboxWithOutCheckboxeslistfield = new InboxWithOutCheckboxeslistfield();
         add(inboxWithOutCheckboxeslistfield);     
         add(new SeparatorField()); 
    }

}

Solution

  • Try This code -

    Add lists on Main Screen-

    InboxWithOutCheckboxeslistfield InboxWithOutCheckboxeslistfield = new InboxWithOutCheckboxeslistfield ();
    add(InboxWithOutCheckboxeslistfield );
    add(new SeparatorField());
    

    Click event listener of list -

    protected boolean navigationClick(int status, int time) {
        getValue();
        return true;
    }
    protected void getValue() {
    Field f = getFieldWithFocus();
    
    if (f instanceof ListField) {
        ListField l = (ListField) f;
    
        final int index = l.getSelectedIndex();
    
        FriendsRequestObject _contactslist = (FriendsRequestObject) FriendsRequest_fields.vector.elementAt(index);
        final String _name = _contactslist.getSender_name();
        final String _id = _contactslist.getSender_id();
        //Dialog.alert(_name);
    
        Application.getApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.alert(_name);
            }
        }); 
        // Dialog.alert("The selected element is: "+Integer.toString(l.getSelectedIndex()));
    }
    }
    
    
    
    
    
    import java.util.Vector;
    import net.rim.device.api.ui.Color;
    import net.rim.device.api.ui.DrawStyle;
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.Font;
    import net.rim.device.api.ui.Graphics;
    import net.rim.device.api.ui.Manager;
    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.component.ListField;
    import net.rim.device.api.ui.component.ListFieldCallback;
    
    class InboxWithOutCheckboxeslistfield extends ListField implements ListFieldCallback {
    
    private Vector contacts;
    static Vector vector = new Vector();
    
    private int contactslist_size = 0;
    
    private String name_ = "", id_="", status_="";
    
    public InboxWithOutCheckboxeslistfield () {
        super(0, ListField.MULTI_SELECT);
        setRowHeight(60);
        setEmptyString("Empty List", DrawStyle.HCENTER);
        setCallback(this);
    
        contacts = new Vector();
    
        vector.addElement(new FriendsRequestObject("1", "hai1", ""));
        vector.addElement(new FriendsRequestObject("2", "hai2", ""));
        vector.addElement(new FriendsRequestObject("3", "hai3", ""));
        vector.addElement(new FriendsRequestObject("4", "hai4", ""));
    
        contactslist_size = vector.size();
    
        for (int x = 0; x < contactslist_size; x++) {
    
            FriendsRequestObject b = (FriendsRequestObject) vector.elementAt(x);
    
            id_ = b.getSender_id().toString();
            name_ = b.getSender_name().toString();
            status_ = b.getimage().toString();
    
    
    
            TableRowManager row = new TableRowManager() {
                public void paint(Graphics g) {
                     g.setBackgroundColor(Color.BLACK);
                    g.fillRect(0, 0, getWidth(), getHeight());
                     g.setColor(Color.BLACK);
                     g.clear();
                     super.paint(g);
                }
            };
    
            LabelField name1 = new LabelField(id_, DrawStyle.ELLIPSIS);
            name1.setFont(Font.getDefault().derive(Font.PLAIN));
            row.add(name1);
    
            LabelField name = new LabelField(name_, DrawStyle.ELLIPSIS);
            name.setFont(Font.getDefault().derive(Font.PLAIN));
            row.add(name);
    
            // add to the table
            contacts.addElement(row);
        }
    
        setSize(contacts.size());
    }
    
    // ListFieldCallback Implementation
    public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
        InboxWithOutCheckboxeslistfield  list = (InboxWithOutCheckboxeslistfield ) listField;
        TableRowManager rowManager = (TableRowManager) list.contacts.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) {
            // Arrange the cell fields within this row manager.
            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();
        }
    
        // Arrages this manager's controlled fields from left to right within
        // the enclosing table's columns.
        protected void sublayout(int width, int height) {
            // set the size and position of each field.
            int fontHeight = Font.getDefault().getHeight();
            int preferredWidth = getPreferredWidth();
    
            /* positioning of fields in list */
            // Status
            Field field = getField(0);
            layoutChild(field, preferredWidth, 50);
            setPositionChild(field, 10, 5);
    
    
            // Name
             field = getField(1);
             layoutChild(field, 150, fontHeight + 1);   
            //setPositionChild(field, 1, 2);
            setPositionChild(field, 80, fontHeight );
            setExtent(preferredWidth, getPreferredHeight());
                    }
    
        // The preferred width of a row is defined by the list renderer.
        public int getPreferredWidth() {
            return Graphics.getScreenWidth();
        }
    
        // The preferred height of a row is the "row height" as defined in the
        // enclosing list.
        public int getPreferredHeight() {
            return getRowHeight();
        }
    
    }
    
    public Object get(ListField listField, int index) {
        return null;
    }
    
    public int getPreferredWidth(ListField listField) {
        return 0;
    }
    
    public int indexOfList(ListField listField, String prefix, int start) {
        return 0;
    }
    
    }
    

    FriendsRequestObject is given below -

    public class FriendsRequestObject {
    
    private String sender_id;
    private String sender_name;
    private String image;
    
    public FriendsRequestObject(String sender_id, String sender_name, String image){
        this.sender_id =  sender_id;
        this.sender_name =sender_name;
        this.image =image;
    }
    
    
    
    public String getimage() {
        return image;
    }
    public void setimage(String image) {
        this.image = image;
    }
    
    public String getSender_id() {
        return sender_id;
    }
    public void setSender_id(String sender_id) {
        this.sender_id = sender_id;
    }
    
    public String getSender_name() {
        return sender_name;
    }
    public void setSender_name(String sender_name) {
        this.sender_name = sender_name;
    }
    
    }