【问题标题】:In Listfield not able to view the fields in blackberry在 Listfield 中无法查看黑莓中的字段
【发布时间】:2012-05-24 05:18:56
【问题描述】:

我已尝试此链接How to customize a ListField in BlackBerry? 为每个列表行创建具有三个标签字段的 ListField。但我无法看到列表字段项目。但我可以说添加了列表字段。因为在导航上单击我可以得到列表字段的选定索引。请任何人帮助我做错了。我尝试过的代码是......

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

主屏类是

public class InboxWithOutCheckboxes extends MainScreen{

    private InboxWithOutCheckboxeslistfield inboxWithOutCheckboxeslistfield;

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

}

【问题讨论】:

    标签: blackberry listfield


    【解决方案1】:

    试试这个代码 -

    在主屏幕上添加列表-

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

    列表的点击事件监听器 -

    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 如下 -

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

    【讨论】:

    • 你能告诉这个列表字段如何从列表字段中获取选定的项目并从列表字段中删除列表项。
    【解决方案2】:

    编辑TableRowManager 的实现。您需要更改getPreferredWidth()getPreferredHeight()。如果 sublayout() 方法执行没有提前完成,getWidth()getHeight() 将返回错误值。

    // 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 getRowHeight();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多