can't find volume of a room using dart - dart

is there any method to find a volume of a room in dart language (idk if it's language)
import 'dart:io';
class Room {
int? height;
int? length;
int? width;
int volume(){
return (height!* length! * width!);
}
}
void main(){
Room room = Room();
room.height = 10;
room.length = 4;
room.width = 5;
print("Volume is ${room.volume()}.")Ж
}
this is the code i have which is working, but teacher said that there is its own method and i should be with it.
if you got a code for it, id be thankful, itd be even better if you plus have a url link of a site where i could learn about it.

I'm not don't quite understand your question but you could make your code simpler by making length, width and height required parameters for a room constructor. Like this:
class Room {
int height;
int length;
int width;
//constructor with required parameters
Room({
required this.width,
required this.height,
required this.length,
});
int volume() {
return (height * length * width);
}
}
void main() {
Room room = Room(
length: 4,
width: 5,
height: 10,
);
print("Volume is ${room.volume()}.");
}
Hope this helps :)

Related

Null safety in flutter and Dart

class CalculatorBrain {
final int height;
final int weight;
double _bmi;
CalculatorBrain({required this.height, required this.weight});
String calculateBMI() {
double _bmi = weight / pow(height / 100, 2);
return _bmi.toStringAsFixed(2);
}
String getResult() {
if (_bmi >= 25) {
return "OverWeight";
} else if (_bmi > 18.5 && _bmi < 25) {
return "Normal";
} else {
return "UnderWeight";
}
}
}
I have try adding late keyword and has used double ? _bmi but none of them works.
This works:
class CalculatorBrain {
final int height;
final int weight;
late double _bmi;
CalculatorBrain({required this.height, required this.weight});
String calculateBMI() {
_bmi = weight / ((height / 100) * (height / 100));
return _bmi.toStringAsFixed(2);
}
String getResult() {
if (_bmi >= 25) {
return "OverWeight";
} else if (_bmi > 18.5 && _bmi <25) {
return "Normal";
}
else {
return "UnderWeight";
}
}
}
void main() {
final brain = CalculatorBrain(height: 180, weight: 80);
brain.calculateBMI();
print(brain.getResult());
}
However, it's horrible class design. I will assume this is a school exercise and you are not developing this as a professional.
A slightly better class design, that doesn't rely on late variables to hide the fact that there is some dark secret involved where the class only works if the programmers knows the magic order in which the functions must be called:
class BodyMassIndex {
final int height;
final int weight;
final double value;
BodyMassIndex({required this.height, required this.weight})
: value = (weight / ((height / 100) * (height / 100)));
String get meaning {
if (value >= 25) {
return "OverWeight";
} else if (value > 18.5 && value <25) {
return "Normal";
}
else {
return "UnderWeight";
}
}
}
void main() {
final bmi = BodyMassIndex(height: 180, weight: 80);
print("value: ${bmi.value}");
print("meaning: ${bmi.meaning}");
}
As a guideline, null-safety depends on you telling your compiler in no uncertain terms what they are supposed to do. If your compiler cannot understand you, it probably means you did not do a good job explaining it, aka "programming". So if you have problems with null safety, it isn't actually with null-safety, it is with your program logic and how you wrote it.
remove the double keyword from the below function:
String calculateBMI() {
double _bmi = weight / pow(height / 100, 2);
...
and change your class property double _bmi to late double _bmi

labelField.getWidth() returns 0? Why does this happen?

I'm working on my own custom manager, and I've gotten it complete so far, but it setsMargins using a percentage of the screen resolution.
Here's how I call the following class:
LabelIconCommandManager licm3 = new LabelIconCommandManager("Address blah bklahblah ", 0);
licm3.add(new ImageButtonField(b1, b2, b3, Field.FIELD_LEFT | ImageButtonField.CONSUME_CLICK));
Here's the class [I've marked in a comment where it returns 0 and where it returns 219. please tell me why this happens:
public class LabelIconCommandManager extends HorizontalFieldManager implements BCMSField
{
LabelIconCommandManager me = this;
EvenlySpacedHorizontalFieldManager buttonManager = new EvenlySpacedHorizontalFieldManager(0);
LabelField labelField;
int side = 0;
int HPADDING = 3;
int VPADDING = 4;
int screenWidth = Display.getWidth();
int labelField_width = 40;
public LabelIconCommandManager()
{
this("", 0);
}
public LabelIconCommandManager(String label, long style)
{
super(USE_ALL_WIDTH| FOCUSABLE);
this.setBorder(BorderFactory.createBitmapBorder(new XYEdges(15, 20, 15, 20),Bitmap.getBitmapResource( "border_edit.png" )));
this.setMargin(1,10,1,10);
labelField = new LabelField(label,LabelField.ELLIPSIS)
{
public void layout(int width, int height)
{
// Done because otherwise ellipses dont work with labelfields
super.layout((int)(screenWidth * 0.61), getHeight());
setExtent((int)(screenWidth * 0.61), getHeight());
labelField_width = labelField.getWidth();
DisplayDialog.alert("labelField_width = " + labelField_width); // returns 219
}
};
// Top Right Bottom Left
labelField.setMargin(VPADDING, HPADDING, VPADDING, 0);
// super because we want this horizontalfieldManager to add it
super.add(labelField);
super.add(buttonManager);
}
public void alternateConstructor(Attributes atts)
{
labelField = new LabelField(atts.getValue("label"), 0);
}
public void onFocus(int direction)
{
this.setBorder(BorderFactory.createBitmapBorder(new XYEdges(15, 20, 15, 20),Bitmap.getBitmapResource( "border_edit_select.png" )));
// uses the same color as listStyleButtonField selections
this.setBackground(BackgroundFactory.createSolidBackground(0x186DEF));
super.onFocus(direction);
}
//Invoked when a field loses the focus.
public void onUnfocus()
{
//top, right,bottom,left
this.setBorder(BorderFactory.createBitmapBorder(new XYEdges(15, 20, 15, 20),Bitmap.getBitmapResource( "border_edit.png" )));
this.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.GRAY, 0));
super.onUnfocus();
invalidate();
}
// Overrride this managers add function
public void add(Field imageButton)
{
// Add a button to the evenly spaced manager
buttonManager.add(imageButton);
// Based on how many buttons there are, set the margin of where the manager holding the buttons start [offset from labelField]
if(buttonManager.getFieldCount() == 1)
{
//side = (int)(screenWidth * 0.1388);
side = screenWidth - labelField_width - 32 - 10 - 15;
DisplayDialog.alert("Screen Width = " + screenWidth);
DisplayDialog.alert("labelField_width2 = " + labelField_width); // returns 0
DisplayDialog.alert("Side = " + side);
}
else side = (int)(screenWidth * 0.05);
buttonManager.setMargin(0,0,0,side);
}
public int getLabelWidth()
{
return labelField_width;
}
}
Here's a picture just to be more clear:
Note: when I ran your code, I didn't actually see labelField_width set to 0. You initialize the value to 40 in the code you posted above. So, I do sometimes see it set to 40, or 219 (on a 360 px wide screen).
But, the problem is that I think you're trying to access the value of labelField_width too soon. The only place it's properly assigned is in the layout() method of your anonymous LabelField. Just because you declare and implement the layout() method in line with the instantiation, doesn't mean that it's called when the LabelField is created. This is actually one of the reasons I don't like anonymous classes.
Anyway, this code:
LabelIconCommandManager licm3 = new LabelIconCommandManager("Address blah bklahblah ", 0);
licm3.add(new ImageButtonField(b1, b2, b3, Field.FIELD_LEFT | ImageButtonField.CONSUME_CLICK));
Will first instantiate the LabelField (inside the LabelIconCommandManager constructor). As I said, that does not trigger the layout() method. The second line above (add()) will trigger your overridden method:
// Overrride this managers add function
public void add(Field imageButton)
{
which is where you see the bad value for labelField_width. That method gets called before layout(). That's the problem.
Since it looks like you only use that width to set the buttonManager margin, you could just wait a little longer to do that. If you wait until the LabelIconCommandManager sublayout() method is called, your LabelField will have had its layout() method called, and labelField_width assigned correctly:
protected void sublayout(int maxWidth, int maxHeight) {
// make sure to call superclass method first!
super.sublayout(maxWidth, maxHeight);
// now, we can reliably use the label width:
side = screenWidth - labelField_width - 32 - 10 - 15;
buttonManager.setMargin(0,0,0,side);
}
That method goes in the LabelIconCommandManager class. And then, you can remove the other place you call buttonManager.setMargin().
Some brief summary from Nate post.
When you construct manager and add fields don't expect that it will be layouted correctly. Manager doesn't know the context - where it will be placed. So layout method for field will be called only when you add his manager to the screen (when layout for manager will be also called). And this is correct.
Move the calculation of your side variable to layout method.
If you really need side value before you put manager to screen. You could precalculate it by using Field.getPrefferedWidth() which returns meaningful values for standard fields (getFont().getAdvance(text) for LabelField, probably also with borders please check yourself). But be careful with this values.
Please review code below. It's manager which has label and buttons. And it puts label at the left side and buttons at the right.
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.decor.Border;
import java.util.Vector;
public class TabFieldManager extends Manager {
public TabFieldManager(long style) {
super(style);
}
protected void sublayout(int width, int height) {
LabelField label = null;
Vector tabs = new Vector();
int tabsWidth = 0;
int tabHeight = 0;
int tabPaddingTop = 0;
int tabPaddingLeft = 0;
for (int i=0; i < getFieldCount(); i++) {
Field field = getField(i);
if (field instanceof LabelField) {
label = (LabelField) field;
} else if (field instanceof ButtonField){
tabs.addElement(field);
layoutChild(field, width, height);
int fieldwidth = field.getWidth() > 0 ? field.getWidth() : field.getPreferredWidth() ;
tabsWidth += fieldwidth + getBorderAndPaddingWidth(field);
int fieldHeight = field.getHeight() > 0 ? field.getHeight() : field.getPreferredHeight();
if (fieldHeight > tabHeight) {
tabHeight = getBorderAndPaddingHeight(field) + fieldHeight;
}
int fieldPaddingTop = field.getPaddingTop();
if (fieldPaddingTop > tabPaddingTop) {
tabPaddingTop = fieldPaddingTop;
}
int fieldPaddingLeft = field.getPaddingLeft();
if (fieldPaddingLeft > tabPaddingLeft) {
tabPaddingLeft = fieldPaddingLeft;
}
}
}
if (label != null) {
layoutChild(label, width - tabsWidth, height);
int y = tabHeight - label.getHeight() >> 1;
setPositionChild(label, tabPaddingLeft , y);
}
for (int i = 0; i < tabs.size(); i++) {
Field tabField = (Field) tabs.elementAt(i);
setPositionChild(tabField, width - tabsWidth, getBorderAndPaddingHeight(tabField));
tabsWidth -= tabField.getWidth() + getBorderAndPaddingWidth(tabField);
}
setExtent(width, tabHeight);
}
private int getBorderAndPaddingHeight( Field field ) {
int height = field.getPaddingTop() + field.getPaddingBottom();
Border border = field.getBorder();
if( border != null ) {
height += border.getTop() + border.getBottom();
}
return height;
}
private int getBorderAndPaddingWidth( Field field ){
int width = field.getPaddingLeft() + field.getPaddingRight();
Border border = field.getBorder();
if( border != null ) {
width += border.getLeft() + border.getRight();
}
return width;
}
protected int moveFocus(int amount, int status, int time) {
if ((status & Field.STATUS_MOVE_FOCUS_VERTICALLY) == Field.STATUS_MOVE_FOCUS_VERTICALLY && amount > 0) {
return amount;
} else
return super.moveFocus(amount, status, time);
}
protected int nextFocus(int amount, int axis) {
if (amount > 0 && axis == Field.AXIS_VERTICAL)
return -1;
else
return super.nextFocus(amount, axis);
}
}

Custom BitmapField bug on unfocus and scroll (BlackBerry)

I have been having this annoying problem when trying to implement a picture gallery on BlackBerry 6.
Everything works, however when the focus changes from the top buttons to say the pictures further down the screen, the images seem to glitch and not paint themselves correctly. Please see the images below for an example:
(Focus is on the top of the screen(not shown))
(Focus is now on the bottom left image, note that the top image is now blank for an unknown reason)
And this happens no matter how many pictures I add to the tumbnail gallery.
Now here is my code, (a part of it concerning the drawing of the thumbnails)
public ProductImage(String productName){
super(VERTICAL_SCROLL|VERTICAL_SCROLLBAR);
currentProduct = productName;
createGUI();
}
public void createGUI(){
deleteAll();
try{
Storage.loadPicture();
}catch(NullPointerException e){
e.printStackTrace();
}
this.setTitle(new LabelField(_resources.getString(PRODUCT_IMAGE), Field.FIELD_HCENTER));
if(ToolbarManager.isToolbarSupported())
{
Toolbar tb = new Toolbar();
setToolbar(tb.createToolBar());
}
else{
Toolbar tb = new Toolbar();
add(tb.createNavBar());
}
picVector = Storage.getPicture(currentProduct);
EncodedImage enc = EncodedImage.getEncodedImageResource("camera.png");
EncodedImage sizeEnc = ImageResizer.sizeImage(enc, Display.getHeight(), Display.getHeight());
takenPicture = new BitmapField(enc.getBitmap());
vfMain = new VerticalFieldManager();
vfMain.add(logo);
vfMain.add(new SeparatorField());
add(vfMain);
prepareBmpFields();
}
private void prepareBmpFields() {
System.out.println("This is the vector size: " + picVector.getPicVector().size());
LayoutManager manager = new LayoutManager();
FieldChangeListener itemListener = new ButtonListener();
mBmpFields = new ImageButtonField[picVector.getPicVector().size()];
for (int i = 0; i < picVector.getPicVector().size(); i++) {
/*EncodedImage image = EncodedImage
.getEncodedImageResource((String)imageVector.elementAt(i));*/
byte[] data = getData((String)picVector.getPicVector().elementAt(i));
//Encode and Resize image
EncodedImage eImage = EncodedImage.createEncodedImage(data,0,data.length);
eImage = ImageResizer.resizeImage(eImage, mImgWidth, mImgHeight);
ImageButtonField currentImage = new ImageButtonField(eImage.getBitmap());
currentImage.setAssociatedPath((String)picVector.getPicVector().elementAt(i));
mBmpFields[i] = currentImage;
mBmpFields[i].setChangeListener(itemListener);
manager.add(mBmpFields[i]);
}
vfMain.add(manager);
}
private class LayoutManager extends VerticalFieldManager {
public LayoutManager() {
super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
}
protected void sublayout(int width, int height) {
int columns = mScrWidth / (mImgWidth + 2 * mImgMargin);
int scrWidth = Display.getWidth();
int rows = mBmpFields.length / columns
+ (mBmpFields.length % columns > 0 ? 1 : 0);
int counter = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin;
int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin;
if(mBmpFields.length > counter){
Field field = mBmpFields[counter];
layoutChild(field, mImgWidth, mImgHeight);
setPositionChild(field, posX, posY);
counter++;
};
}
}
if(Display.getWidth() < Display.getHeight()){
setExtent(mScrWidth, (int)(mScrHeight*1.25));
}
else{
setExtent(mScrWidth, (int)(mScrHeight*2));
}
}
public int getPreferredWidth() {
return mScrWidth;
}
public int getPreferredHeight() {
return mScrHeight;
}
}
}
I have removed many non relevant parts of the code, but the needed code is there.
Does anyone know what could be causing this problem? Thanks for your help!
Edit: as requested, here is my implementation of ImageButtonField class:
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.BitmapField;
public class ImageButtonField extends BitmapField{
String associatedPath ="";
BitmapField image2;
public ImageButtonField(Bitmap image) {
super(image);
}
public void setAssociatedPath(String path){
associatedPath = path;
}
public String getAssociatedPath(){
return associatedPath;
}
public boolean isFocusable() {
return true;
}
protected void applyTheme(Graphics arg0, boolean arg1) {
}
protected void drawFocus(Graphics graphics, boolean on) {
}
protected void onFocus(int direction) {
// only change appearance if this button is enabled (aka editable)
if (isEditable()) {
invalidate(); // repaint
}
super.onFocus(direction);
}
public void onUnfocus() {
invalidate(); // repaint
super.onUnfocus();
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean trackwheelClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected void paint(Graphics graphics) {
super.paint(graphics);
if (isFocus()) {
graphics.setGlobalAlpha(128);
graphics.setColor(0x888888);
graphics.fillRect(0, 0, getWidth(), getHeight());
}else{
graphics.setGlobalAlpha(0);
graphics.setColor(0x000000);
graphics.fillRect(0, 0, getWidth(), getHeight());
//graphics.drawBitmap(0, 0, getWidth(), getHeight(), image2.getB, 0, 0);
}
}
protected boolean keyChar(char character, int status, int time) {
if(Characters.ENTER == character || Characters.SPACE == character) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}
Ok, so you can disregard my first answer, but since I didn't have your ImageButtonField code at the time, I don't want to throw it out ... maybe someone else will find it useful.
In the end, I didn't need to make any changes to ImageButtonField, but I did change your LayoutManager class. The way I figured out that it was the problem was I just started replacing your custom UI classes with built-in ones. I replaced ImageButtonField with BitmapField. That didn't fix it. Then, I replaced LayoutManager with FlowFieldManager and that fixed it. So, I knew where the problem was.
My solution:
private class LayoutManager extends Manager {
public LayoutManager() {
super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
}
protected void sublayout(int width, int height) {
setExtent(width, height);
// TODO: maybe always set the same virtual extent?
if (Display.getWidth() < Display.getHeight()) {
setVirtualExtent(mScrWidth, (int) (mScrHeight * 1.25));
} else {
setVirtualExtent(mScrWidth, (int) (mScrHeight * 2));
}
int columns = mScrWidth / (mImgWidth + 2 * mImgMargin);
// int scrWidth = Display.getWidth();
int rows = mBmpFields.length / columns + (mBmpFields.length % columns > 0 ? 1 : 0);
int counter = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin;
int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin;
if (mBmpFields.length > counter) {
Field field = mBmpFields[counter];
layoutChild(field, mImgWidth, mImgHeight);
setPositionChild(field, posX, posY);
counter++;
}
}
}
}
public int getPreferredWidth() {
return mScrWidth;
}
public int getPreferredHeight() {
return mScrHeight;
}
}
I can't say for sure that I understand why your original code wasn't working, but I can say that I wouldn't have done a few of the things in the original code:
The original code was extending VerticalFieldManager but was doing all the work itself, in sublayout(). So, I don't think there was any point extending VerticalFieldManager. I changed it to just extend Manager.
The original code was calling setExtent() with different sizes. I don't think that's what you wanted. Extent is the actual size of the Field. Virtual extent is the virtual size, which is what you want to set larger than the actual extent, in order to enable scrolling. You don't need to dynamically calculate different extents for portrait vs. landscape because the width and height parameters passed to sublayout() will already reflect that. I'm not sure you really even need to be setting different virtual extents either. I think you should probably always set the virtual extent height to the number of rows times picture height, accounting for margins.
You had an unused variable scrWidth in your original code. I commented it out above.
You also posted this question recently, right? Am I correct in assuming that the ImageButtonField you refer to here is the same one you were working on in the other question?
I can't see your full implementation of ImageButtonField, which you should probably post here, too. However, looking at the answers to your other question, I have a feeling that you're doing some custom focus handling in ImageButtonField, and maybe it's not being done quite right. In any case, that class may be where the problem is.
I have a similar Field subclass of my own, and here are the focus handling methods I define:
public class CustomButtonField extends Field {
private Bitmap _button; // the currently displayed button image
private Bitmap _on; // image for 'on' state (aka in-focus)
private Bitmap _off; // image for 'off' state (aka out-of-focus)
protected void onFocus(int direction) {
// only change appearance if this button is enabled (aka editable)
if (isEditable()) {
_button = _on;
invalidate(); // repaint
}
super.onFocus(direction);
}
protected void onUnfocus() {
_button = _off;
invalidate(); // repaint
super.onUnfocus();
}
protected void drawFocus(Graphics graphics, boolean on) {
// override superclass implementation and do nothing
}
public boolean isFocusable() {
return true;
}
I also have a custom implementation of paint(). I won't show it all here, because a lot of the code probably has nothing to do with your problem, but my paint() does include this call:
graphics.drawBitmap(_padding, _padding, _fieldWidth, _fieldHeight, _button, 0, 0);
You might not care about the fact that I have separate images for focused, and unfocused states ... maybe you show the same image at all times.
But, probably the thing to check is your onFocus() and onUnfocus() methods. You may need to add a call to invalidate() as I have.
Looking at Rupak's answer to your other question, it would also be good to check your ImageButtonField.paint() method, and make sure you aren't neglecting to do important drawing steps if the field is not in focus.

BlackBerry Torch scrolling issue

I have created an app which is working fine on BlackBerry bold but when I installed it on Torch the screen is not scrolling..
I have used Manager with enabled vertical scrolling and added it to another master vertical field manager?
Did any one get this issue before ??
the code for the manager is below
public class TableManager extends Manager {
public int HEIGHT = 0;
public TableManager(int height) {
super(Manager.USE_ALL_WIDTH|Manager.VERTICAL_SCROLL);
HEIGHT = height;
}
public int getPreferredWidth() {
return Constants.width * 90 / 100;
}
public int getPreferredHeight() {
return HEIGHT;
}
protected void sublayout(int maxWidth, int maxHeight) {
int y = 150;
try{
int count = getFieldCount();
for (int i = 0; i < count; i++) {
Field field = getField(i);
layoutChild(field, field.getPreferredWidth(), field
.getPreferredHeight());
setPositionChild(field, (Constants.width - field
.getPreferredWidth()) >> 1, y);
y += field.getPreferredHeight();
}
setExtent(maxWidth, HEIGHT);
}catch(Exception e){
e.printStackTrace();
}
}
}
Try to add your TableManager to a master manager that is not vertically Scrollable.
You may also use a simple VerticalFieldManager with any Field inside set to FIELD_HCENTER instead of using this TableManager.

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) {
// TODO 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.

Resources