BlackBerry listfield development [closed]

余生颓废 提交于 2019-12-11 08:28:40

问题


Can anybody suggest me how to create a clickable list field in blackberry such that on clicking one item a new screen must appear.


回答1:


I've done a similar work with VerticalFieldManager and custom extended Field on JDE 5.0.

I assume that you have a list of objects that you wanted to display on main screen.

First; create a class which extends Field for your list's item, override its paint method, layout method and otuher events as your requirements.

Next, create a main screen that you wanted to show the list. Once you have populated your object list, in a loop, pass each object model to previously created field's constructor. Then add fieldChanged event to that field and add it to verticalFieldManager.

You need to override the events (like fieldChanged event) as you want to click on it and display its detail on another screen.

Finally, create a detail screen that takes required arguments to display your list item's object detail. On fieldChanged event of your main screen implementation, pass your object to detail screen and push the detail screen.

Also, this approach may be useful for you.

Example:

custom field:

public class CListItemField extends Field {

    private CListItemModel model;

    public CListItemField(CListItemModel _model, long style) {
        super(style);
        this.model = _model;
    }

    public CListItemModel getModel() {
        return this.model;
    }

    // overrides

    public int getPreferredHeight() {
        //return custom height
    }

    public int getPreferredWidth() {
        //return custom width
    }

    protected void layout(int width, int height) {
        setExtent(Math.min(width, getPreferredWidth()), getPreferredHeight());
    }

    protected void paint(Graphics g) {

        //custom paint stuff (borders, fontstyle, text position, icons etc.)

        if (isFocus()) {
            //focused item display settings

        } else {
            //item display settings
        }

    }

    protected void drawFocus(Graphics graphics, boolean on) {
    }

    public boolean isFocusable() {
        return true;
    }

    protected void onFocus(int direction) {
        super.onFocus(direction);
        invalidate();
    }

    protected void onUnfocus() {
        super.onUnfocus();
        invalidate();
    }

    protected boolean navigationClick(int status, int time) {
        fieldChangeNotify(0);
        return true;
    }

    protected boolean keyChar(char character, int status, int time) {
        //send key event to listener
        if (character == Keypad.KEY_ENTER) {
            fieldChangeNotify(0);
            return true;
        }
        return super.keyChar(character, status, time);
    }

}

list screen:

public class ScreenListbox extends MainScreen implements FieldChangeListener, FocusChangeListener {

private VerticalFieldManager verticalField;
private Vector listItemVector;

public ScreenOttoInbox(String title) {
    super(title, Manager.NO_VERTICAL_SCROLL);
    setData();
    setComponents();
}

private void setData() {
    //populate listItemVector according to your business (ie. read json response then parse it and collect it to a vector)
}

public void setComponents() {
    verticalField = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
    setListContent(verticalField, listItemVector);
    add(verticalField);
}

private void setListContent(VerticalFieldManager field, Vector vector) {
    try {
        int vlen = vector.size();
        for (int i = 0; i < vlen; i++) {
            CListItemModel model = (CListItemModel) vector.elementAt(i);
            CListItemField itemField = new CListItemField(model, Field.FOCUSABLE | Field.ACTION_INVOKE);
            itemField.setChangeListener(this);
            itemField.setFocusListener(this);
            field.add(itemField);
        }
    } catch (Exception ex) { }
}

protected boolean onSavePrompt() {
    return true;
}

public void fieldChanged(Field field, int context) {
    //custom field's click/touch event handler
    CListItemField itemField = (CListItemField) field;
    ScreenItemDetail scrDetail = new ScreenItemDetail(itemField.getModel());
    ScreenUtil.pushScreenWithLoader(scrDetail,true);
}

protected void onDisplay() {
    super.onDisplay();
}
 }



回答2:


Create a class like below.

 import java.util.Vector;
    import net.rim.device.api.collection.util.SparseList;
    import net.rim.device.api.system.Bitmap;
    import net.rim.device.api.ui.Color;
    import net.rim.device.api.ui.Graphics;
    import net.rim.device.api.ui.component.ListField;
    import net.rim.device.api.ui.component.ListFieldCallback;

    public class CListCallback  implements ListFieldCallback {
    private String[] resource;
    private int rgb=Color.BLACK;
    Vector elements;
    Bitmap arraow;
    public CListCallback(String[] resources){
        this.resource=resources;

    }
        public void drawListRow(ListField listField, Graphics graphics, int index,
                int y, int width) {

           String text=(String) get(listField, index);
            graphics.setColor(rgb);
            graphics.drawText(text,60,y+25);  
             graphics.drawLine(0, y+59, DConfig.disWidth, y+59);

        }
      public Object get(ListField listField, int index) {

            return resource[index];
        }
   public int getPreferredWidth(ListField listField) {

             return DConfig.disWidth+10;
        }
    public int indexOfList(ListField listField, String prefix, int start) {

            return -1;
        }

    }

And use above class in MainScreen class.

CListCallback clmenu=new CListCallback(arrayitems);
      final ListField lf = new ListField(arraymenu.length) {
            protected boolean keyChar(char character, int status, int time) {
                if (character == Keypad.KEY_ENTER) {
                    fieldChangeNotify(0);
                    return true;
                }
                return super.keyChar(character, status, time);
            }

            protected boolean navigationUnclick(int status, int time) {
                fieldChangeNotify(0);
                return true;
            }
        };
        lf.setCallback(clmenu);
        lf.setRowHeight(60);
        lf.setChangeListener(new FieldChangeListener() {

            public void fieldChanged(Field field, int context) 
            {
            int index=lf.getSelectedIndex();
                                 UiApplication.getUiApplication.pushScreen(newNewScreen(imgarray[index]));  
            }
        });
        add(lf);    

Thats it.it will work



来源:https://stackoverflow.com/questions/7414522/blackberry-listfield-development

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!