0

任何人都可以建议我如何在黑莓中创建一个可点击的列表字段,以便在点击一个项目时必须出现一个新屏幕。

4

2 回答 2

1

创建一个如下所示的类。

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

    }

并在 MainScreen 类中使用上述类。

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

就是这样。它会工作

于 2011-09-17T11:09:16.503 回答
1

我在 JDE 5.0 上使用 VerticalFieldManager 和自定义扩展字段完成了类似的工作。

我假设您有一个要在主屏幕上显示的对象列表。

第一的; 为您的列表项创建一个扩展 Field 的类,根据您的要求覆盖其绘制方法、布局方法和其他事件。

接下来,创建一个要显示列表的主屏幕。填充对象列表后,在循环中将每个对象模型传递给先前创建的字段的构造函数。然后将 fieldChanged 事件添加到该字段并将其添加到verticalFieldManager。

您需要覆盖事件(如 fieldChanged 事件),因为您想单击它并在另一个屏幕上显示其详细信息。

最后,创建一个详细信息屏幕,该屏幕采用所需的参数来显示列表项的对象详细信息。在主屏幕实现的 fieldChanged 事件上,将您的对象传递给详细屏幕并推送详细屏幕。

此外,这种方法可能对您有用。

例子:

自定义字段:

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

}

列表屏幕:

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();
}
 }
于 2011-09-14T10:22:53.213 回答