ListField的方法invalidate不起作用

时间:2013-01-06 13:55:12

标签: blackberry java-me invalidation listfield

我正在编写一个自定义列表字段,但是当我调用invalidate方法时它不起作用。屏幕始终为空,而列表字段的数据不为空或空! 请帮我弄清楚问题。
提前谢谢

public class FileListField extends ListField implements ListFieldCallback{

    private static final Bitmap fileBitmap = Bitmap.getBitmapResource("document.png");
    private static final Bitmap dirBitmap = Bitmap.getBitmapResource("emty_folder.png");
    private static final Bitmap addBitmap = Bitmap.getBitmapResource("svn_added.png");
    private static final Bitmap delBitmap = Bitmap.getBitmapResource("svn_deleted.png");
    private static final Bitmap modBitmap = Bitmap.getBitmapResource("svn_modified.png");
    private static final Bitmap novBitmap = Bitmap.getBitmapResource("svn_noversion.png");
    private static final Bitmap norBitmap = Bitmap.getBitmapResource("svn_normal.png");
    //static Bitmap dowBitmap = Bitmap.getBitmapResource("svn_added.png");

    private BigVector bv;

    public FileListField(){
        super();
        bv = new BigVector();
        setCallback(this);
    }

    public void setFileModelVector(BigVector fileModelVector){
        bv.removeAll();
        bv.addElements(fileModelVector.getContiguousArray());
        invalidate();
        System.out.println("ISSSSSSSSSSSSSS CALLLLLLLLLLINNNNNNNG?");
        System.out.println("DISSSPATH?"+UiApplication.isEventDispatchThread());
//      invalidateRange(0, bv.size()-1);
//      FileModel[] fileModelArr = new FileModel[fileModelVector.size()];
//      for(int i=0;i<fileModelVector.size();i++)
//          fileModelArr[i] = (FileModel)fileModelVector.elementAt(i);
//      set(fileModelArr);
    }

    public BigVector getFileModelVector(){
//      BigVector bv = new BigVector();
//      for(int i=0;i<this.getSize();i++)
//          bv.addElement(get(this, i));
        return bv;
    }

    public void set(Object[] or){

    }

    public Object get(ListField list, int index){
        return bv.elementAt(index);
    }

    public int getSize(){
        return bv.size();
    }

    public int indexOfList(ListField listField, String prefix, int start) {
        return getSelectedIndex();
    }

    public int getPreferredWidth(ListField listField) {
        return Display.getWidth();
    }

    public void drawListRow(ListField listField, Graphics graphics
            , int index, int y, int width){
        System.out.println("DRWAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        FileModel fileModel = (FileModel)get(listField, index);
        if(fileModel.isFile())
            graphics.drawBitmap(0, y, width, fileBitmap.getHeight()
                    , fileBitmap, 0, 0);
        else
            graphics.drawBitmap(0, y, width, dirBitmap.getHeight()
                    , dirBitmap, 0, 0);
        switch(fileModel.getStatus()){
            case FileModel.ADD_STATUS:
                graphics.drawBitmap(0, y, width, addBitmap.getHeight()
                        , addBitmap, 0, 0);
                break;
            case FileModel.DEL_STATUS:
                graphics.drawBitmap(0, y, width,delBitmap.getHeight()
                        , delBitmap, 0, 0);
                break;
            case FileModel.MOD_STATUS:
                graphics.drawBitmap(0, y, width, modBitmap.getHeight()
                        , modBitmap, 0, 0);
                break;
            case FileModel.DOW_STATUS:
                break;
            case FileModel.NOR_STATUS:
                graphics.drawBitmap(0, y, width, norBitmap.getHeight()
                        , norBitmap, 0, 0);
                break;
            default:
                graphics.drawBitmap(0, y, width, novBitmap.getHeight()
                        , novBitmap, 0, 0);
                break;
        }
        graphics.drawText(fileModel.getName(), fileBitmap.getWidth(), y);
    }

}

1 个答案:

答案 0 :(得分:0)

我认为在invalidate()上调用ListField就不够了。 invalidate()调用确实是View layer方法,表示必须重新绘制字段。你真正在做的是表明Model已经改变了。

See this other question on Stack Overflow

尝试使用setSize():

将代码更改为此类代码
public void setFileModelVector(BigVector fileModelVector){
    bv.removeAll();
    bv.addElements(fileModelVector.getContiguousArray());
    //invalidate();
    setSize(bv.size());
}

我也不知道你究竟是如何处理载体本身的所有权,但你也可能只是这样做:

public void setFileModelVector(BigVector fileModelVector){
    bv = fileModelVector;
    //invalidate();
    setSize(bv.size());
}

因为您正在清除整个矢量,然后将新矢量转换为数组,然后将数组添加到原始矢量。但是,这是一个单独的问题,并不是你看到空ListField的原因。