ProgressIndicator not animating on blackberry - blackberry

I want to display progress indicator on screen when images loading from web. i'm used below code.,
Bitmap bitmap ;
BitmapField imageField ;
VerticalFieldManager vfm= new VerticalFieldManager();
ProgressAnimationField spinner =new ProgressAnimationField(Bitmap.getBitmapResource("spinner2.png"));
vfm.add(spinner);
new Thread(new Runnable()
{
public void run()
{
bitmap = new UrlToImage(Constants.ICON_URL+"_big.png"+suffix).getImage();
imageField = new BitmapField(bitmap);
vfm.delete(spinner)
vfm.add(imagefield);
}
}).start();
If i execute , it throws illegalStateException. Again i change to ui thead.,
Bitmap bitmap ;
BitmapField imageField ;
VerticalFieldManager vfm = new VerticalFieldManager();
ProgressAnimationField spinner =new ProgressAnimationField(Bitmap.getBitmapResource("spinner2.png"));
vfm.add(spinner);
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
bitmap = new UrlToImage(Constants.ICON_URL+"_big.png"+suffix).getImage();
imageField = new BitmapField(bitmap);
vfm.delete(spinner)
vfm.add(imagefield);
}
});
No error display. but the progressanimationfield not animated.
What to change on my code for animate the progressAnimatedField. Pls give any idea..

I think the problem is that you are trying to do both UI and non-UI operations on the UI thread. So, your UrlToImage is holding the UI thread hostage while it is retrieving data from the network, preventing all other UI operations (including re-painting your animated progress indicator). What about something like this:
Bitmap bitmap ;
BitmapField imageField ;
VerticalFieldManager vfm = new VerticalFieldManager();
ProgressAnimationField spinner =new ProgressAnimationField(Bitmap.getBitmapResource("spinner2.png"));
vfm.add(spinner);
new Thread(new Runnable()
{
public void run()
{
bitmap = new UrlToImage(Constants.ICON_URL+"_big.png"+suffix).getImage();
imageField = new BitmapField(bitmap);
UiApplication.getUiApplication().invokeLater(new Runnable()
public void run() {
vfm.delete(spinner);
vfm.add(imagefield);
}
});
}
});

I know this is already marked as answered, but I was thinking you should look at my post about this subject here
It should make all your application heartbeat problems go away.

Related

List field in blackberry , event click listener not working

I am working on BB OS v5.0. I have managed to get the list to appear on the screen. I am getting data from webservice and adding it into a Vector.
Now I want to find out onclick, which is the item that is clicked and accordingly perform some operation. For that i am trying to display an alert. But I'm not getting the alert.
Here is my code :
In my mainscreen , i added fieldmanager=new VerticalFieldManager(); and add(fieldmanager);
void fetchAlbumsForLetter(String letter) {
Status.show("Processing ....", 3000);
fieldManager.deleteAll();
VerticalFieldManager top = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR | Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR | Field.USE_ALL_WIDTH){
public void paint(Graphics graphics) {
graphics.setBackgroundColor(0x00290008);
graphics.setColor(Color.WHITE);
graphics.clear();
graphics.drawBitmap(0, 0, sha.getWidth(),
sha.getHeight(), sha, 0, 0);
super.paint(graphics);
}
};
add(top);
CustomListField4 list4 = new CustomListField4(null){
protected boolean navigationClick(int status, int time) {
getValue4();
return true;
}
};
fieldmanager.add(list4);
}
protected void getValue4() {
Field f = getFieldWithFocus();
if (f instanceof ListField) {
ListField l = (ListField) f;
final int index = l.getSelectedIndex();
HistoryItem _contactslist = (HistoryItem) CustomListField4.val4.elementAt(index);
final String id = _contactslist.getName();
Dialog.alert(id+"");
}
}
Please help me to resolve this
EDIT
class CustomListField4 extends ListField implements ListFieldCallback {
public CustomListField4(Vector data) {
super(0, ListField.MULTI_SELECT);
final TableRowManager row = new TableRowManager() {
public void paint(Graphics g) {
// g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0f3e19b);
g.clear();
super.paint(g);
}
};
Bitmap icon = Bitmap.getBitmapResource("Devil Skype.png");
HorizontalFieldManager h=new HorizontalFieldManager();
h.add(new BitmapField(icon));
//h.add(new BitmapField(song.getThumb()));
h.add(new LabelField(song.getAlbumName()));
//h.add(new LabelField(row1.getLanguage()));
//h.setMargin(0,0,50,0);
//Dialog.alert(song.getName());
VerticalFieldManager vfm=new VerticalFieldManager();
vfm.add(h);
//vfm.add(new LabelField(song.getArtist()));
row.add(vfm);
contacts.addElement(row);
}
setSize( contacts.size());
}
// ListFieldCallback Implementation
public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
listField.setRowHeight(index,107);
CustomListField4 list = (CustomListField4) listField;
TableRowManager rowManager = (TableRowManager) CustomListField4.contacts.elementAt(index);
rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}
public class TableRowManager extends Manager {
public TableRowManager() {
super(0);
}
You are calling getFieldWithFocus() which will give you the manager. You need to get the leaf field
protected void getValue4() {
Field f = getLeafFieldWithFocus();
if (f instanceof ListField) {
//Your code
}
}
I think your hierarchy of Field and Manager objects is incorrect, and this is causing problems with your detection of field focus/selection.
It wasn't obvious from the original code you posted, but by looking at your update, I assume that you are calling fetchAlbumsForLetter() once for every row. That's not right.
fetchAlbumsForLetter() is creating a new CustomListField4 each time it's called. And, CustomListField4 is a ListField.
A ListField is not meant to represent only one row. It's meant to represent all the rows. You should only create one instance of CustomListField4.
I would do either one of two things:
1. Continue to Use a ListField
If you want CustomListField4 to be a ListField (extends ListField), then in your implementation of
public void drawListRow(ListField listField, Graphics g, int index, int y, int width);
you should actually draw graphics objects, using all the Graphics#draw methods. These are primitive graphics items, like filled areas, lines, text, or bitmaps. You would not be using Field objects inside each ListField row, as you're trying to do with your TableRowManager class.
See here for a sample ListField, or here for a more sophisticated example
2. Imitate ListField with a Manager
change your code to
public class CustomListField4 extends VerticalFieldManager {
or
public class CustomListField4 extends Manager {
Then, you can use a TableRowManager for each row, and add LabelField or BitmapField objects to it.
See here for an example of this
If you fix these problems, then I think the way you are overriding navigationClick() will work fine for detecting the row click, and doing something with the selected row.
You can try this
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert(id+"");
}
});

Blackberry Field Manager Scrolling Shows NullpointerException

I am having problem with Blackberry horizontal and vertical Field Managers. In my sample application I have two vertical field managers added to one horizontal field manager and the GUI is in the following figure.
In the field change listener of button I am removing the left VFM, then only the Home button is displayed, and in home button listener I am adding the left VFM, then it get back to the above screen.
The problem is occurring after scrolling the left content and deleting the left VFM, Then only Home button is displayed and track pad navigation (scrolling) leads to Null Pointer exception screen. The following figure contains the simulator screen shot of error.
And here is the complete source code for reproducing the error.
public class MyApp extends UiApplication{
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
/**
* Creates a new MyApp object
*/
public MyApp()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new MyScreen());
}
}
public final class MyScreen extends MainScreen{
private HorizontalFieldManager hfmMainManager = null;
private VerticalFieldManager menuButtonManager = null;
private String[] buttons = new String[]{"BUTTON ID 1",
"BUTTON ID 2",
"BUTTON ID 3",
"BUTTON ID 4",
"BUTTON ID 5",
"BUTTON ID 6",
"BUTTON ID 7",
"BUTTON ID 8",
"BUTTON ID 9",
"BUTTON ID 10"};
private boolean menuShowing = false;
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
super( Manager.NO_VERTICAL_SCROLL | MainScreen.NO_VERTICAL_SCROLLBAR
| Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR);
hfmMainManager = new HorizontalFieldManager(Manager.NO_HORIZONTAL_SCROLL
| Manager.NO_HORIZONTAL_SCROLLBAR);
////////////////////MENU BUTTONS
menuButtonManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR);
menuButtonManager.add(new ButtonField("Header Button", Field.FIELD_HCENTER));
VerticalFieldManager vfmScrollable = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
for (int i = 0; i < buttons.length; i++) {
ButtonField buttonField = new ButtonField(buttons[i], Field.FIELD_HCENTER);
buttonField.setChangeListener(listenerButton);
vfmScrollable.add(buttonField);
}
menuButtonManager.add(vfmScrollable);
hfmMainManager.add(menuButtonManager);
VerticalFieldManager vfmMenuContentManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLLBAR | Manager.NO_VERTICAL_SCROLL);
ButtonField buttonField = new ButtonField("Home Button", Field.FIELD_HCENTER);
buttonField.setChangeListener(listenerButton);
buttonField.setMargin(10, 0, 10, 0);
vfmMenuContentManager.add(buttonField);
hfmMainManager.add(vfmMenuContentManager);
add(hfmMainManager);
menuShowing = true;
}
private FieldChangeListener listenerButton = new FieldChangeListener() {
public void fieldChanged(Field arg0, int arg1) {
synchronized (UiApplication.getEventLock()) {
if(menuShowing){
menuShowing = false;
hfmMainManager.delete(menuButtonManager);
}else{
menuShowing = true;
hfmMainManager.insert(menuButtonManager, 0);
}
}
}
};
}
How do I solve this Issue?
Thanks in advance
Regards
Anish
I've seen similar behavior when trying to change the field structure from a focus or unfocus event. When a field removes itself from the screen in the middle of the focus event, the focus tracking can get out of sync, and cause unusual errors like this. Try changing from grabbing the UI lock to calling invokeLater instead. That way the change listener can finish executing before you delete the VFM, keeping all the invariants in place.
Get rid of this:
synchronized (UiApplication.getEventLock()) {
Replace it with this:
UiApplication.getInstance().invokeLater(new Runnable{ public void run() {

Is there any LazyLoader for images to load image in ListField in BlackBerry?

I am new to BlackBerry development. But good about android.
I want to load Images coming from the server in ListField.
I have implement like below code but not getting success:
package mypackage;
public class TempScreen extends MainScreen implements ListFieldCallback{
Bitmap[] images=null;
private ListField mylist;
private static Bitmap _bitmap;
private ImageDownloader downloader;
int size = 0;
String[] urls={
"http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg",
"http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg",
"http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg",
"http://www.kentnews.co.uk/polopoly_fs/damian_lewis_at_port_lympne_wild_animal_park_c_taf_1_1738362!image/2626063106.jpg_gen/derivatives/landscape_225/2626063106.jpg"};
public TempScreen()
{
images=new Bitmap[urls.length];
size = urls.length;
mylist = new ListField();
mylist.setCallback(this);
mylist.setSize(4);
mylist.setRowHeight(getFont().getHeight() * 3);
add(mylist);
Thread downloader=new Thread(new ImageDownloader());
downloader.start();
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
if(images[index]==null)
{
//Load placeholder image
_bitmap = Bitmap.getBitmapResource("close_btn.png");// load some bitmap
// of your choice
// here
}
else
//Load Bitmap
_bitmap = images[index];
graphics.drawText("row details", 100, y + 30);
//graphics.drawBitmap(0, y, _bitmap.getWidth(), _bitmap.getHeight(),_bitmap, 0, 0);
mylist.invalidate(index);
}
public class ImageDownloader implements Runnable
{
public void run()
{
for(int i=0; i<size;i++)
{
if(images[i]==null)
{
images[i]=connectServerForImage(urls[i].toString());//replace downloadImage method to whatever method you have to download the bitmap from url
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run()
{
mylist.invalidate();
}
});
}
}
}
}
public Object get(ListField listField, int index) {
// TODO Auto-generated method stub
return null;
}
public int getPreferredWidth(ListField listField) {
// TODO Auto-generated method stub
return 0;
}
public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return 0;
}
public static Bitmap connectServerForImage(String url) {
HttpConnection httpConnection = null;
DataOutputStream httpDataOutput = null;
InputStream httpInput = null;
int rc;
Bitmap bitmp = null;
try {
// httpConnection = (HttpConnection)
// Connector.open(url+";interface=wifi");
httpConnection = (HttpConnection) Connector.open(url);
rc = httpConnection.getResponseCode();
// System.out.println("===============================");
Dialog.alert("beore if condition");
if (rc == HttpConnection.HTTP_OK) {
System.out.println(" ============= IN FUNCTION. . . . .");
httpInput = httpConnection.openInputStream();
InputStream inp = httpInput;
byte[] b = IOUtilities.streamToBytes(inp);
EncodedImage hai = EncodedImage.createEncodedImage(b, 0,
b.length);
bitmp = hai.getBitmap();
} else {
throw new IOException("HTTP response code: " + rc);
}
} catch (Exception ex) {
System.out.println("URL Bitmap Error........" + ex.getMessage());
} finally {
try {
if (httpInput != null)
httpInput.close();
if (httpDataOutput != null)
httpDataOutput.close();
if (httpConnection != null)
httpConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return bitmp;
}
}
Dont know where i am wrong. Please can any budy help me for the same.
Several problems with your code:
The BitmapLazyLoader class looks like a consumer. It holds a Thread reference. This alone is very confusing, since Runnables are intended to be passed to a Thread constructor, but Runnables should not know about the thread for the sake of encapsulation. Letting this apart, this class attempts to spawn a thread only once, but as you are creating an instance of Runnable each time a row is drawn, you'll end up spawning a considerable number of threads. This will probably end in a TooManyThreadsException being thrown as in BlackBerry the max number of threads is limited to 16 per app. Even if you don reach the limit, performance will degrade as BlackBerries, which sport a single core CPU, you shouldn't have more than 2-3 threads running at the same time. EVEN if you could spawn infinite threads, in BlackBerry you can only have X connections opened at the same time (I think X is 5 for the whole OS, not sure about this). So first of all modify the code to ensure only a single worker thread is downloading images. (and if possible, extract the thread instantiation and launch out of the Runnable class).
When the bitmap is downloaded, you are not doing anything with it. Look at the ImageDownloadCompleted method, it is empty. (BTW, the convention for methods is to start with lowercase) So you should store the bitmap somewhere and call invalidate on your list, which in turn will paint the stored bitmaps.
Hope it helps.
You can try using this link :
http://www.coderholic.com/blackberry-webbitmapfield/
You have to create a separate class named as WebBitmapField as suggested in above link.
How to use that class in your list field image objects:
For every image url create WebBitmapField object
photoList_vector is the vector through which populate elements in
list field
WebBitmapField web = new WebBitmapField("http://www.image1.png");
photoList_vector.addElement(web);
web = new WebBitmapField("http://www.image2.png");
photoList_vector.addElement(web);
Now use this vector to work on your list field......
In the above lines we try to ensure that when we simultaneously send multiple requests to get the images then each image corresponds to a particular WebBitmapField Object.
Each object is then added to vector so that it can be added to the list field.
Each url send is tied to an object of WebBitmapField.
So though request is send in a separate thread it gets tied to its associated object only
Hope it helps
:)
I have worked on this problem, earlier, and I am posting my technique here, though its not ideal solution, as it was coupled very much with Screen class, but still might be helpful.
First in your screen class have one array for bitmaps having size equal to list field items.
public class TempScreen extends MainScreen{
Bitmap[] images=null;
String[] urls={"image1_url", "image2_url".....};
public TempScreen()
{
images=new Bitmap[urls.length];
}
now in drawListRow method of ListFieldCallBack, check for the following:
public void drawListRow(ListField list, Graphics g, int index, int y, int width){
if(bitmap[index]==null)
{
//Load placeholder image
}
else
//Load Bitmap
}
Now create a thread class to download the images:
public class ImageDownloader implements Runnable
{
public void run()
{
for(int i=0; i<size;i++)
{
if(images[i]==null)
{
images[i]=downloadImage(url[i]);//replace downloadImage method to whatever method you have to download the bitmap from url
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run()
{
list.invalidate()
}
});
}
}
}
}
Now in constructor of the screen class, after setting callback to listfield, start thread:
Thread downloader=new Thread(new ImageDownloader());
downloader.start();
Edit: Change TempScreen constructor to following:
public TempScreen()
{
images=new Bitmap[urls.length];
size = urls.length;
mylist = new ListField();
mylist.setCallback(this);
mylist.setSize(4);
mylist.setRowHeight(getFont().getHeight() * 3);
add(mylist);
Thread downloader=new Thread(new ImageDownloader());
downloader.start();
}

Vaadin cropfield selection stick to top

In vaadin application I use CropField addon to crop user's avatar. The problem is that whenever I try to test it, selection area is always stick to the top of the cropfield. Everything was fine on one page but when I copied exactly that piece of code to another page the problem has occured. Do you have any ideas why?
NativeButton nb = new NativeButton("Click on me");
layout.addComponent(nb);
nb.addListener(new ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
final Window window = new Window();
window.setModal(true);
DivLayout cropContent = new DivLayout();
window.setContent(cropContent);
final CropField cropField = new CropField(new ExternalResource("http://upload.wikimedia.org/wikipedia/commons/1/1e/Stonehenge.jpg"));
cropField.setSelectionAspectRatio(1);
cropContent.addComponent(cropField);
//cropField.setImmediate(true);
NativeButton btClose = new NativeButton("Close");
btClose.addListener(new ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
getMyApplication().getMainWindow().removeWindow(window);
}
});
cropContent.addComponent(btClose);
getMyApplication().getMainWindow().addWindow(window);
}
});

Setting a background color to my Blackberry application very basic!

This is my screen:
final class GeneralExpenseViewScreen extends MainScreen {
public GeneralExpenseViewScreen() {
super();
LabelField title = new LabelField("TeamMate TEC | Expenses",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
Background bg = BackgroundFactory.createSolidBackground(0xBDBDDB);
setBackground(bg);
HorizontalFieldManager headerAreaManager = new HorizontalFieldManager();
HorizontalFieldManager filterAreaManager = new HorizontalFieldManager();
HorizontalFieldManager expenseListAreaManager = new HorizontalFieldManager();
HorizontalFieldManager totalAreaManager = new HorizontalFieldManager();
HorizontalFieldManager addNewAreaManager = new HorizontalFieldManager();
add(headerAreaManager);
add(filterAreaManager);
add(expenseListAreaManager);
add(totalAreaManager);
add(addNewAreaManager);
/**Begin form layouts**/
Bitmap headerImage = Bitmap.getBitmapResource("sergioheader.png");
BitmapField header = new BitmapField(headerImage);
headerAreaManager.add(header);
}
public boolean onClose() {
Dialog.alert("AH!");
System.exit(0);
return true;
}
}
Notice that I'm calling setBackground directly to the class but it's not working how I think it would work.
How can I set a background color to my application form?
Thanks.
I've used this code with success:
protected void paint(Graphics graphics) {
graphics.setBackgroundColor(0xBDBDDB);
graphics.clear();
super.paint(graphics);
}
Depending on the version you're developing for, you could use the following
getMainManager().setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
to set the screen managers background color.

Resources