问题
I'm writing a custom list field, but it doesn't not work when i invoke invalidate method. The screen is always empty, while the data of list field is not null or empty!
Please help me figure out the problem.
Thank you in advance
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:
I don't think calling invalidate()
on your ListField
is enough. The invalidate()
call really is a View layer method, that indicates that the field must be repainted. What you are really doing is indicating that the Model has changed.
See this other question on Stack Overflow
Try changing your code to something like this, using setSize():
public void setFileModelVector(BigVector fileModelVector){
bv.removeAll();
bv.addElements(fileModelVector.getContiguousArray());
//invalidate();
setSize(bv.size());
}
I also don't know how exactly you're handling ownership of the vector itself, but you could also probably just do this:
public void setFileModelVector(BigVector fileModelVector){
bv = fileModelVector;
//invalidate();
setSize(bv.size());
}
since you were clearing out the entire vector, then converting the new vector to an array and then adding the array to the original vector. But, that's a separate issue, and isn't the reason you are seeing an empty ListField
.
来源:https://stackoverflow.com/questions/14182804/listfields-method-invalidate-does-not-work