开发者

How can I using Array, List ,HashMap or HashSet - create long list of BitmapFields? (Java, Blackberry)

开发者 https://www.devze.com 2023-03-14 11:03 出处:网络
I need to:1) create long list of BitmapFields and 2) add them to the screen. Since list is long I want to use some shor开发者_开发百科t automated method like Loop or similar:

I need to: 1) create long list of BitmapFields and 2) add them to the screen. Since list is long I want to use some shor开发者_开发百科t automated method like Loop or similar:

while (i < 1000)
 {
 i = i + 1;
 myBitmapField[i].setBitmap(Bitmap.getBitmapResource("picture" + i+ ".png"));
 myVerticalFieldManager.add(_myBitmapField[i]);
 }

But it seems that I cannot assign a index i to a name of BitmapField myBitmapField[i], only to a name of the file itself.

So how can I create a long list of BitmapFields ? Can i use List, Array, HashMap, or HashSet for this purpose? An example welcome. Thanks a lot! (Blackberry, Java)


If you're getting that error, that just means that myBitmapField isn't declared as an array type. You'll want to declare it as an array, e.g:

BitmapField[] myBitmapField = new BitmapField[1000];
for (int i = 0; i < 1000; i++) {
    myBitmapField[i] = new BitmapField();
}

You could in principle use any of the data types you listed, though only an array can be indexed with the [...] syntax. You just have to change the declaration initialization to use whatever data type you want. And if you use a type that doesn't support indexes you'll have to change your index to use the .get method on the data type you choose. But I'm not sure why you'd want to use anything other than an array...

And at that point you might as well combine the above loop with your existing loop, so that you only have one loop.

Also, if you have 1000 images being displayed, you will probably have horrible performance. BlackBerry has trouble dealing with 1000 fields of any type in a list, and images can be fairly heavyweight. It may even take so long to load on some devices that the BlackBerry will think your app is hanging and terminate it. Not to mention that it isn't very good from a UI perspective to have so many items on a small mobile screen, since no user would be able to navigate to all of them in a reasonable timeframe.


I tried your way, doesn't seem to for for me either. try this:

private pics = new Vector();

for(int i = 0; i < 1000; i++{

    BitmapField temp = new BitmapField();
    pics.addElement(temp);
    ((BitmapField)pics.elementAt(i)).setBitmap(Bitmap.getBitmapResource("picture" + i + ".png"));
    add((BitmapField)pics.elementAt(i));

}

Also i agree with @Ted about the 1000 fields. good luck, let us know.

0

精彩评论

暂无评论...
验证码 换一张
取 消