0

我正在开发一个应用程序,我需要在其中按日期显示产品列表。所以在列表中应该首先出现日期,然后是产品项目。现在我的问题是布局,我很困惑如何制作这个布局?这是页面..

在此处输入图像描述

根据此屏幕,那里有日期和产品详细信息,因此我需要在代码中更改哪些更改..

这是我的代码

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
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.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.Touchscreen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

class UiMainscreen extends MainScreen implements FieldChangeListener
{
    private CustomListField cu_field[];
    private Bitmap image=null;
    int size=8;
    public UiMainscreen() {
        VerticalFieldManager vmanager=new VerticalFieldManager(VERTICAL_SCROLL|VERTICAL_SCROLLBAR){
            protected void sublayout(int maxWidth, int maxHeight) {

                super.sublayout(Display.getWidth(),Display.getHeight());
                setExtent(Display.getWidth(),Display.getHeight());
            }
        };
        cu_field=new CustomListField[size]; 
        for(int i=0;i<size;i++){
            image=Bitmap.getBitmapResource("sample_"+i+".jpg");
            cu_field[i]=new CustomListField("BlackBerry models that had a built-in mobile phone, were the first models that natively ran Java, and transmitted data over the normal 2G cellular network. RIM began to advertise these devices as email-capable mobile phones rather than as 2-way pagers.", image, "jan2011", 100, 100,Color.LIGHTGREEN);
            cu_field[i].setChangeListener(this);
            vmanager.add(new SeparatorField());
            vmanager.add(cu_field[i]);
        }
        add(vmanager);
    }

    public void fieldChanged(Field field, int context) {
        // TODO Auto-generated method stub
        for(int i=0;i<size;i++){
            if(field==cu_field[i]){
                final int k=i;
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        Dialog.alert("You click on Item No "+k);
                    }
                });
            }
        }
    }
}

class CustomListField extends HorizontalFieldManager{

    private Bitmap scale_image;
    private int width=0;
    private int height=0;
    private int background_color=0;
    private BitmapField bitmap_field;
    private boolean flag=false;
    public CustomListField(String title, Bitmap image, String date,int image_width,int image_height,int background_color){
        super(NO_HORIZONTAL_SCROLL|USE_ALL_WIDTH);
        this.background_color=background_color;
        width=image_width;
        height=image_width;
        if(image!=null){
            scale_image=new Bitmap(image_width, image_height);
            image.scaleInto(scale_image, Bitmap.FILTER_LANCZOS);
            bitmap_field=new BitmapField(scale_image);
            flag=false;
            bitmap_field.setMargin(5, 5, 5, 5);
            add(bitmap_field);
        }


        VerticalFieldManager vmanager=new VerticalFieldManager(USE_ALL_WIDTH|Field.FIELD_VCENTER){
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(Display.getWidth()-width, height);
                setExtent(Display.getWidth()-width, height);
            }
        };
        //HorizontalFieldManager HFMTOPlist  =new HorizontalFieldManager(Field.FIELD_HCENTER|Field.FIELD_TOP);
        Bitmap TopStrip = Bitmap.getBitmapResource("logo36X36.png");
        BitmapField bitmapStrip = new BitmapField(TopStrip);
       // HFMTOPlist.add(bitmapStrip);

       // vmanager.add(HFMTOPlist);

        LabelField title_lbl=new LabelField("Title: "+title,Field.NON_FOCUSABLE|DrawStyle.ELLIPSIS);
        vmanager.add(title_lbl);

        LabelField date_lbl=new LabelField("Date: "+date,Field.NON_FOCUSABLE);
        vmanager.add(date_lbl);
        vmanager.add(bitmapStrip);
        Font fon=title_lbl.getFont();
        int title_height=fon.getHeight();

        Font font=date_lbl.getFont();
        int date_height=font.getHeight();
        int pad=title_height+date_height;
        title_lbl.setPadding((height-pad)/2, 0, 0, 0);
        add(vmanager);
        add(new NullField(FOCUSABLE));
    }

    protected void sublayout(int maxWidth, int maxHeight) {
        super.sublayout(Display.getWidth(), height);
        setExtent(Display.getWidth(), height);
    }
    protected void paint(Graphics graphics) {
        if(flag)
        graphics.setBackgroundColor(background_color);
        graphics.clear();
        super.paint(graphics);
    }
    protected void onFocus(int direction) {
        super.onFocus(direction);
        flag=true;
        invalidate();
    }
    protected void onUnfocus() {
        invalidate();
        flag=false;
    }
    protected boolean navigationClick(int status, int time) {

        if(Touchscreen.isSupported()){
            return false;
        }else{
            fieldChangeNotify(1);
            return true;
        }

    }
    protected boolean touchEvent(TouchEvent message) 
    {
        if (TouchEvent.CLICK == message.getEvent()) 
        {

            FieldChangeListener listener = getChangeListener();
            if (null != listener)
                this.setFocus();
                listener.fieldChanged(this, 1);
        }
        return super.touchEvent(message);
    }
}
4

1 回答 1

1

布局是 ( LabelField+ ListField) 对的组合。实现一个列表列表作为您的数据模型,并实现一个Comparator实例以根据您的规则对列表列表中的数据进行排序。

然后根据您的数据模型通过添加LabelField和实例组成一个屏幕,并在您的数据模型更改时更新这些字段。ListField

请注意,在此处发布较长的源代码表无助于解决问题。通常,当有人在代码方面寻求帮助时,他会自己进行调查并发布一小段代码(约 10 行)并寻求特定的帮助。

于 2012-08-16T10:08:19.110 回答