How to control manager class in Blackberry
Dear All, I have a problem when creating a UI on Blackberry.
First, i try to create a ChatLayoutManager class extended from Manager class. My layout has three component: topfield, mainfield and bottom field.
public class ChatLayoutManager extends Manager {
private Field bottomField;
private Field mainField;
private Field titleField;
public ChatLayoutManager(long style) {
super(style);
}
protected void sublayout(int width, int height) {
setExtent(width, height);
int y = 0;
if (bottomField != null) {
layoutChild(bottomField, width, height);
// This goes at the bottom of the screen
setPositionChild(bottomField, 0, height-bottomField.getHeight());
height -= bottomField.getHeight();
}
if (titleField != null) {
layoutChild(titleField, width, height);
// This goes at the top of the screen
setPositionChild(titleField, 0, 0);
height -= titleField.getHeight();
y += titleField.getHeight();
}
if (mainField != null) {
layoutChild(mainField, width, height);
// This goes just below the title field (if any)
setPositionChild(mainField, 0, y);
}
}
public void setMainField(Field f) {
mainField = f;
add(f);
}
public void setBottomField(Field f) {
bottomField = f;
add(f);
}
public void setTitleField(Field f) {
titleField = f;
add(f);
}
Then i create another field (ChatField) extended from manager to add to mainfield in the ChatLayoutManager class which i have created above.
public class ChatField extends Manager{
private Field _contentField[];
protected ChatField(){
super(Manager.HORIZONTAL_SCROLL | Manager.VERTICAL_SCROLL);
}
// TODO Auto-generated constructor stub}
protected synchronized void sublayout(int width, int height) {
// TO开发者_C百科DO Auto-generated method stub
setExtent(width, height);
int x = 0;
int y = 0;
if(_contentField.length > 0){
for(int i = 0 ;i<_contentField.length; i++){
//if(getManager() == this){
this.layoutChild(_contentField[i],
_contentField[i].getWidth(),
_contentField[i].getHeight());
this.setPositionChild(_contentField[i], x, y);
if(_contentField[i++]!= null){
if ((_contentField[i].getWidth() + _contentField[i].getWidth())
>= width){
x = 0;
y += _contentField[i].getHeight();
}
else{
x += _contentField[i].getWidth();
}
}
//}
}
}
}
public void setContentField(Field field[]){
_contentField = field;
}
}
And now, when i create some fields(such as TextField, BitmapField ...) added to ChatField, the program has an error "Field is not a child of this manager". The reason is when the framework invokes the sublayout function of the ChatField class , when sublayout starts calling layoutChild function the manager of field is not ChatField but ChatlayoutManager.
I've experience hard time trying to resolve this problem, still I have no solution. Anybody can give me some suggestions? I really appreciate.
When you call Manager.add() (passing in a Field) that field becomes a child of that manager. A field can only be a child of a single Manager - it cannot belong to multiple Managers. If you're getting that error, then you may be accidentally adding it to more than one Manager.
Your setContentField(Field[]) function gets a reference to the field array provided, but it never adds the content of the array to the manager (ChatField). This is why you get the error that the field is not a child of this manager.
Secondly, with respect to your ChatField#sublayout, you should do one of the following:
1) Instead of referencing the _contentField array directly, use functionality provided by Manager to reference its children:
int numFields = getFieldCount(); // getFieldCount() is a member of Manager
int marginHorizontal = 0;
for(int i = 0; i < numFields; i++) {
field = getField(i); // getField() is a member of Manager
// Whatever you need to do to it.
}
2) If you would still rather reference _contentField directly, or if you need control over a specific field, then make sure that the Field's manager is the manager being laid out:
for(int i = 0 ;i<_contentField.length; i++){
if(_contentField[i] != null && _contentField[i].getManager() == this){
// Whatever you need to do to it.
}
}
Hope this helps.
精彩评论