0

我正在做一个程序,很简单。您只需在提供的字段中输入一个人的姓名。单击搜索,它会搜索数据库并检索信息。包括图片。这工作正常,但我有一个问题。如果我使用彼得进行搜索并且数据库中有两个“彼得”。当我滚动。除非我单击图片字段,否则图片不会更改。我相信我发布了相关代码。任何帮助将不胜感激。谢谢

public class DriverSearchResultScreen extends MainScreen implements FieldChangeListener{
    LabelField header,title;

    LabelField lblLicenseNumber,lblFirstName,lblLastName,lblBirthDate,lblLicenseExpiry,lblLicenseType,lblLicenseStatus;

    ColoredMultiLineLabelField clblLicenseNumber,clblFirstName,clblLastName,clblBirthDate,clblLicenseExpiry,clblLicenseType,clblLicenseStatus;

    CustomButtonField btnSearch;

    CustomImageButton btnLeft,btnRight;

    CustomImageButton bmpfDriverPhoto;

    Vector licenseInfoData;

    int currentIndex = 0;

    public DriverSearchResultScreen(Vector licenseInfoData ) {

        this.licenseInfoData = licenseInfoData;
        // TODO Auto-generated constructor stub
        title = new LabelField("License",LabelField.USE_ALL_WIDTH);
        /*header.setBackground(bg);*/
        setTitle(title);

        bmpfDriverPhoto = new CustomImageButton(Bitmap.getBitmapResource("default-profile.png")){
            public boolean isFocusable() { return true; };
        };
        bmpfDriverPhoto.setChangeListener(this);
        add(bmpfDriverPhoto);

        header = new LabelField("",LabelField.FIELD_HCENTER|LabelField.USE_ALL_WIDTH){
                public void paint(Graphics g){
                    int col = g.getColor();
                    g.setColor(Color.YELLOW);
                    super.paint(g);
                    g.setColor(col);
                }
                public int getPreferredWidth(){
                    return net.rim.device.api.system.Display.getWidth();
                }
            };
        this.getMainManager().setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("background.png")));
        header.setFont(Font.getDefault().derive(Font.SANS_SERIF_STYLE|Font.BOLD,25));

        add(header);
        add(new SeparatorField());

        Bitmap vfmBackground = Bitmap.getBitmapResource("rounded-border.png");
        VerticalFieldManager vfm = new VerticalFieldManager();
        vfm.setBorder(BorderFactory.createBitmapBorder(new XYEdges(12,12,12,12),vfmBackground));

        BHorizontalFieldManager hfm = new BHorizontalFieldManager();
        lblFirstName= new LabelField("First Name :");
        hfm.add(lblFirstName);
        clblFirstName = new ColoredMultiLineLabelField("Jhon",Color.GRAY);
        hfm.add(clblFirstName);
        vfm.add(hfm);
        vfm.add(new SeparatorField());

        BHorizontalFieldManager hfm1 = new BHorizontalFieldManager();

        lblLastName= new LabelField("Last Name :");
        hfm1.add(lblLastName);
        clblLastName = new ColoredMultiLineLabelField("Doe",Color.GRAY);
        hfm1.add(clblLastName);
        vfm.add(hfm1);
        vfm.add(new SeparatorField());

        BHorizontalFieldManager hfm2 = new BHorizontalFieldManager();

        lblBirthDate = new LabelField("Date Of Birth:");
        hfm2.add(lblBirthDate);
        clblBirthDate = new ColoredMultiLineLabelField("11-June-1982",Color.GRAY);
        hfm2.add(clblBirthDate);
        vfm.add(hfm2);
        vfm.add(new SeparatorField());

        BHorizontalFieldManager hfm3 = new BHorizontalFieldManager();

        lblLicenseNumber = new LabelField("License Number :");
        hfm3.add(lblLicenseNumber);
        clblLicenseNumber = new ColoredMultiLineLabelField("999999",Color.GRAY);
        hfm3.add(clblLicenseNumber);
        vfm.add(hfm3);
        vfm.add(new SeparatorField());


        BHorizontalFieldManager hfm4 = new BHorizontalFieldManager();

        lblLicenseType = new LabelField("License Type :");
        hfm4.add(lblLicenseType);
        clblLicenseType = new ColoredMultiLineLabelField("B",Color.GRAY);
        hfm4.add(clblLicenseType);
        vfm.add(hfm4);
        vfm.add(new SeparatorField());


        BHorizontalFieldManager hfm5 = new BHorizontalFieldManager();

        lblLicenseExpiry = new LabelField("Expiry Date:");
        hfm5.add(lblLicenseExpiry);
        clblLicenseExpiry = new ColoredMultiLineLabelField("25-Aug-2012",Color.GRAY);
        hfm5.add(clblLicenseExpiry);
        vfm.add(hfm5);
        vfm.add(new SeparatorField());


        BHorizontalFieldManager hfm6 = new BHorizontalFieldManager();

        lblLicenseStatus = new LabelField("Licenes Status:");
        hfm6.add(lblLicenseStatus);
        clblLicenseStatus = new ColoredMultiLineLabelField("Expired",Color.GRAY);
        hfm6.add(clblLicenseStatus);
        vfm.add(hfm6);
        vfm.add(new SeparatorField());

        add(vfm);

        btnLeft = new CustomImageButton(Bitmap.getBitmapResource("left-arrow-enable.png"),Bitmap.getBitmapResource("left-arrow-disable.png"));
        btnRight= new CustomImageButton(Bitmap.getBitmapResource("right-arrow-enable.png"),Bitmap.getBitmapResource("right-arrow-disable.png"));;

        HorizontalFieldManager arrowButtonHfm = new HorizontalFieldManager(){

            public int getPreferredHeight() {
                // TODO Auto-generated method stub
                return btnLeft.getPreferredHeight()*2;
            }

            public int getPreferredWidth() {
                // TODO Auto-generated method stub
                return Display.getWidth();
            }

            protected void sublayout(int maxWidth, int maxHeight) {
                // TODO Auto-generated method stub
                int count = getFieldCount();
                for(int i = 0 ; i < count ; i++ ){
                    Field f = getField(i);

                    if(f == btnLeft ){
                        setPositionChild(f, (getPreferredWidth() >> 1) - f.getPreferredWidth() - 30, getPreferredHeight()>>1);
                    }else if (f == btnRight ){
                        setPositionChild(f, (getPreferredWidth() >> 1) + 30, getPreferredHeight()>>1);
                    }
                    layoutChild(f, getPreferredWidth(), getPreferredHeight());
                }
                setExtent(getPreferredWidth(),getPreferredHeight());
            }

            public void subpaint(Graphics graphics){
                int count = getFieldCount();
                for(int i = 0 ; i < count ; i++ ){
                    net.rim.device.api.ui.Field f = getField(i);
                    paintChild(graphics,f);
                }
            }

        };

        /*add(new LabelField(){
            public int getPreferredHeight() {
                return 10;
            }
         });*/
        btnLeft.setChangeListener(this);
        btnRight.setChangeListener(this);
        btnLeft.setEnabled(false);
        arrowButtonHfm.add(btnLeft);
        arrowButtonHfm.add(btnRight);
        add(arrowButtonHfm);
        fillLicenseHolderInfoToField((LicenseObject)licenseInfoData.elementAt(currentIndex));
        setHeaderText();
        enableDisableButtons();
    }

    private void setHeaderText(){
        header.setText("Search Results. ( "+(currentIndex+1)+ " of "+ (licenseInfoData.size())+" )");
    }


     private void fillLicenseHolderInfoToField(LicenseObject licenseObject){
        bmpfDriverPhoto.setImage(licenseObject.getBmpImage());
        clblLicenseNumber.setText(licenseObject.getLicenseNum());
        clblFirstName.setText(licenseObject.getFirstName());
        clblLastName.setText(licenseObject.getLastName());
        clblBirthDate.setText(licenseObject.getDateOfBirth());
        clblLicenseExpiry.setText(licenseObject.getDateOfExpiry());
        clblLicenseType.setText(licenseObject.getLicenseType());
        clblLicenseStatus.setText(licenseObject.getLicenseStatus());
    }

    public void fieldChanged(Field field, int context) {
        // TODO Auto-generated method stub
        if(field == btnRight){



            if( currentIndex < (licenseInfoData.size() - 1)){
                currentIndex++;
                fillLicenseHolderInfoToField((LicenseObject)licenseInfoData.elementAt(currentIndex));
            }
        }else if(field == btnLeft){
            if( currentIndex > 0 ){
                currentIndex--;
                fillLicenseHolderInfoToField((LicenseObject)licenseInfoData.elementAt(currentIndex));
            }
        }else if( field == bmpfDriverPhoto ){
            ImagePopDialog dialog = new ImagePopDialog("loader-image-box.png");
            Bitmap scaledBitmap = new Bitmap(300, 300);
            Bitmap btnBitmap    = bmpfDriverPhoto.getImage();
            btnBitmap.scaleInto(0, 0, btnBitmap.getWidth(), btnBitmap.getHeight(),
                        scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), 1);
            dialog.add(scaledBitmap);
            UiApplication.getUiApplication().pushScreen(dialog);

        }

        enableDisableButtons();
        setHeaderText();
    }


    private void enableDisableButtons(){
        btnLeft.setEnabled((currentIndex > 0)?true:false);
        btnRight.setEnabled((currentIndex < (licenseInfoData.size() - 1))?true:false);
    }
    protected boolean onSavePrompt() {
        // TODO Auto-generated method stub
        return true;
    }
}

//自定义图片按钮

package com.app.ui.component;

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.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;

/**
 * * 
 * Class to Draw an Image Button
 */
public class CustomImageButton extends Field {

/** Bitmap for enabled image */
private Bitmap bmpEnabled;

/** Bitmap for disabled image */
private Bitmap bmpDisabled;

/** Label for Button*/
private String label;

private boolean mEnabled=true;

private int mCurrentTouchX = 0;

private int mCurrentTouchY = 0;

/**
 * Constructor with
 * @param image
 */
public CustomImageButton(Bitmap image) {
    super(Field.FOCUSABLE);
    this.bmpEnabled = image;
}

/**
 * Constructor with
 * @param enabled
 * @param disabled
 */
public CustomImageButton(Bitmap enabled,Bitmap disabled) {
    this(enabled);
    this.bmpDisabled = disabled;
}

public CustomImageButton(Bitmap enabled, Bitmap disabled, String label) {
    this(enabled);
    this.bmpDisabled = disabled;
    this.label = label;
}

protected void layout(int height, int width) {
    height = getPreferredHeight();
    width = getPreferredWidth();
    setExtent(width,height);

}

protected void paint(Graphics graphics) {
    if( isEnabled() ){
        if(isFocus()){
            int col = graphics.getColor();
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, getPreferredHeight(), getPreferredHeight());
            graphics.setColor(col);
        }
        graphics.drawBitmap(5, 5, bmpEnabled.getWidth(), bmpEnabled.getHeight(), bmpEnabled,
            0, 0);          
        if(label != null && !label.equals("")) {
            graphics.setColor(Color.BLACK);
            graphics.drawText(label, bmpEnabled.getWidth()+5, (getPreferredHeight() - getFont().getHeight())/2);
        }
    }else{
        if( bmpDisabled != null )
            graphics.drawBitmap(5, 5, bmpDisabled.getWidth(), bmpDisabled.getHeight(), bmpDisabled,
                    0, 0);
        if(label != null && !label.equals("")) {
            graphics.setColor(Color.LIGHTGRAY);
            graphics.drawText(label, bmpEnabled.getWidth()+5, (getPreferredHeight() - getFont().getHeight())/2);
        }
    }
}

public int getPreferredWidth() {
    int prefwidth = 0;
    if(bmpEnabled != null) {    
        prefwidth =  bmpEnabled.getWidth(); 
    }   

    if(label != null && !label.equals("")) {    
        prefwidth = Display.getWidth()+10;
    }

    return (prefwidth+10);
}

public int getPreferredHeight() {
    if(bmpEnabled != null) {
        return bmpEnabled.getHeight()+10;
    }
    return 0;

}

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

/**
 * To set the focused background color and invalidating the entire screen
 */
protected void onFocus(int direction) {
    invalidate();
}

/**
 * To set the unfocused background color and invalidating the entire screen
 */
protected void onUnfocus() {
    invalidate();
}

/**
 * Draws the focus indicator for this field. 
 * @param graphics
 * @param on
 */
protected void drawFocus(Graphics graphics, boolean on) {
    super.drawFocus(graphics, on);
}

protected boolean navigationClick(int status, int time){
    if( mEnabled ){
        if (mCurrentTouchX >= 0 && mCurrentTouchX <= getPreferredWidth()
                && mCurrentTouchY >= 0
                && mCurrentTouchY <= getPreferredHeight())
            fieldChangeNotify(1);

        mCurrentTouchX = 0;
        mCurrentTouchY = 0;
        return true;
    }else{
        return false;
    }
}

protected boolean touchEvent(TouchEvent message) {
    if (mEnabled) {
        if (message.getEvent() == TouchEvent.CLICK) {
            mCurrentTouchX = message.getX(1);
            mCurrentTouchY = message.getY(1);
        }
    }
    return super.touchEvent(message);
}

public void setEnabled(boolean enabled) {
    // TODO Auto-generated method stub
    super.setEnabled(enabled);
    if(!enabled)
        this.focusRemove();
}

public void setImage(Bitmap image){
    this.bmpEnabled = image;
}

public Bitmap getImage(){
    return this.bmpEnabled;
}

}

4

1 回答 1

0

In your CustomImageButton class you should change your setImage method:

public void setImage(Bitmap image){
    this.bmpEnabled = image;
}

by adding an invalidate to cause a redraw:

public void setImage(Bitmap image){
    this.bmpEnabled = image;
    invalidate();
}

The problem is that when your image is replaced, the change isn't seen on screen until the screen has been refreshed. invalidate() triggers a refresh of the screen(or layout), allowing any updates to be seen.

When you scroll or select the image, the invalidate() method is getting called so as the screen refreshes, which is why it isn't updating until then. A manual trigger of invalidate() solves this problem.

Make sure you are doing it on the Event Thread.

于 2013-01-08T15:03:45.197 回答