setChangeListener method is not invoked - blackberry

i am new in blackberry developer. i am use a pillsetbutton and pillfieldbutton
but when i am click pillfieldbutton no any action is performed.I am using setchangeListener() method.but no any Action is performed.i am going Through this process.
public DemoPill() {
PillButtonSet objButtonSet=new PillButtonSet();
final PillButtonField objButtonField1=new PillButtonField("NSE");
final PillButtonField objButtonField2=new PillButtonField("BSE");
objButtonSet.add(objButtonField1);
objButtonSet.add(objButtonField2);
this.add(objButtonSet);
bjButtonSet.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("Hi ");
if(field==objButtonField1)
{
System.out.println("This Is NSE Button");
}
else if(field==objButtonField2)
{
System.out.println("This Is BSE Button");
}
}
});
}
}

You are printing it on console. So without debugging the code you will never know if your click is consumed. So just use an event thread to see the output on your screen. I have provided you the sample just check it. It will show the output on your screen. You can also use Dialog.inform(String message ) But its always good to do it on event thread.
public DemoPill() {
PillButtonSet objButtonSet=new PillButtonSet();
final PillButtonField objButtonField1=new PillButtonField("NSE");
final PillButtonField objButtonField2=new PillButtonField("BSE");
objButtonSet.add(objButtonField1);
objButtonSet.add(objButtonField2);
this.add(objButtonSet);
bjButtonSet.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// System.out.println("Hi ");
if(field==objButtonField1)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("objButtonField1 button clicked")
}
});
}
else if(field==objButtonField2)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("objButtonField2 button clicked")
}
});
}
}
});
}
}
May be this will help cheers. :)

You only can view the output of
System.out.println("ANYDATA");
in debug mode not in run.
Try to debug it not to run it.

Related

Hide status message in Blackberry

A status message can be displayed in Blackberry using method show of class net.rim.device.api.ui.component.Status. Using this method you specify a certain amount of time that the message should be displayed but, is there any way to hide this status message BEFORE this time?
I am using this code for displaying status messages in my application:
public static void status(final String message, final int time) {
UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { Status.show(message, time); } });
}
EDIT: Solution that worked for me (thanks to Eugen Martynov solution)
public static void hideStatus() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Screen activeScreen = UiApplication.getUiApplication().getActiveScreen();
if (activeScreen instanceof Status) {
activeScreen.close();
}
}
});
}
According to javadoc there no concrete method to close status.
However you could try to ask UiApplication.getUiApplication().getActiveScreen() and try to call close() of it

Blackberry - How to make a clock count down and display on screen

I am making a quiz application with question and answer.
I want to make an clock count down ( Minute + Second ) and display it on screen.
But I can not how to do that.
Can anyone help me please ?
this is one solution to print time mm:ss format (Minutes:seconds)
public class StartUp extends UiApplication{
public static void main(String[] args) {
StartUp start=new StartUp();
start.enterEventDispatcher();
}
public StartUp() {
pushScreen(new Timerscreen());
}
}
class Timerscreen extends MainScreen
{
LabelField time;
long mille=0;
Timer timer=null;TimerTask task=null;
public Timerscreen() {
mille=1000*60*1;
time=new LabelField();
add(time);
timer=new Timer();
task=new TimerTask() {
public void run() {
synchronized (UiApplication.getEventLock()) {
if(mille!=0){
SimpleDateFormat date=new SimpleDateFormat("mm:ss") ;
System.out.println("================="+date.formatLocal(mille)+"====================="+Thread.activeCount());
time.setText(date.formatLocal(mille));
mille=mille-1000;
}else{
time.setText("00:00");
mille=1000*60*1;
timer.cancel();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Time expaired");
}
});
}
}
}
};
timer.schedule(task,0, 1000);
}
}
Same like above but some change:
First set:
int secs=120; // how many seconds you want;
public void callTheTimer()
{
label=new LabelField("\t");
add(label);
timer=new Timer();
timerTask=new TimerTask()
{
public void run()
{
synchronized (UiApplication.getEventLock())
{
if(secs!=0)
{
label.setText(""+(secs--)+" secs");
}
else
{
timer.cancel();
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
label.setText("");
Dialog.alert("Times Up");
secs=120;
}
});
}
}
}
};
timer.schedule(timerTask, 0, 1000);
}
This is enough;

listeners for ObjectChoice field

I am creating a BlackBerry application which contains two ObjectChoiceFields. I want to add listeners for each of them. I searched for that but did not find any of useful code.
It is like a country-state selector on websites. I am looking for simple logic, and I have no need of any database oriented solution. Can anyone describe how to add listeners for those two ObjectChoiceField?
I have have attached my code below, but it is only working for country choice field. Nothing happens when I change state choice.
public class MyApplication extends MainScreen implements FieldChangeListener
{
ObjectChouceField choice_c,choice_s;
public MyApplication ()
{
this.setTitle("hai");
choice_c = new MyChoiceField("Select a Country", countryArray);
choice_c.setChangeListener(this);
this.add(choice_c);
choice_s = new MyChoiceField("Select a State", stateArray);
choice_s.setChangeListener(this);
this.add(choice_s);
..................
..................
}
public void fieldChanged(Field field, int context)
{
if(field == choice_category)
{
Dialog.alert("choice country has been pressed");
}
else if(field == choice_round)
{
Dialog.alert("choice state has been pressed");
}
}
}
Have you tried overriding the navigationClick method of the ObjectChoiceField?
Here is an example:
ObjectChoiceField selectionField = new ObjectChoiceField("Select the Country",countryArray)
{
protected boolean navigationClick(int arg0, int arg1)
{
return super.navigationClick(arg0, arg1);
}
};
cf is the ChoiceField. Use the getSelectedIndex to and trigger events accordingly.
cf.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
switch (cf.getSelectedIndex()) {
case 0:
//code here //
break;
default:
break;
}
}
});

blackberry buttonfield navigate on 2 second hold

I am developing a blackberry app. I want to open an screen from the home screen when user will press one button and hold it for 2 seconds.
Any way?
Thanks.
Here is the code for your question. I have make use of this BlackBerry LongClickListener implementation link which contains good explanation too.
public class HoldButtonScreen extends MainScreen implements FieldChangeListener {
ButtonField _btnHold;
Timer _timer;
public HoldButtonScreen() {
_btnHold = new ButtonField("Hold 2 sec to get popup")
{
protected boolean navigationClick(int status, int time) {
final Field _btnHold= this;
_timer = new Timer();
System.out.println("hi there");
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try{
_timer.schedule(new TimerTask() {
public void run() {
fieldChanged(_btnHold, 0);
}}, 2000);
}catch(Exception e){
e.printStackTrace();
}
}
});
return true;
}
protected boolean navigationUnclick(int status, int time) {
System.out.println("hi unclick");
add(new LabelField("You have't hold button for 2 second."));
_timer.cancel();
return true;
}
};
_btnHold.setChangeListener(this);
add(_btnHold);
}
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
PopupScreen _popUpScreen = new PopupScreen(new VerticalFieldManager()){
public boolean onClose() {
close();
return true;
}
};
_popUpScreen.add(new LabelField("Hello , i am pop up after 2 second."));
UiApplication.getUiApplication().pushScreen(_popUpScreen);
}
});
}
}
Try this this will run successfully.
Just change the Screen in PushScreen.
private Thread splashTread;
protected int _splashTime = 200;
boolean countinue = true;
splashTread = new Thread() {
public void run() {
while (countinue == true) {
try {
synchronized (this) {
wait(_splashTime);
}
} catch (InterruptedException e) {
} finally {
synchronized (UiApplication.getUiApplication().getAppEventLock()) {
UiApplication.getUiApplication().pushScreen(new Login());
SplashScreen.this.close();
}
countinue = false;
}
}
}
};
splashTread.start();

how to stop Browserfield requestContent() method when back to mainScreen on blackberry

When i click a button, in next screen i've loaded browserfield requestcontent() using thread.
and i've added browserfield listener.
when click back button , i came to first screen. But in background, requestContent is executing. how to stop it.?
i write the code on onClose() method
public boolean onClose()
{
for(int i=0;i<=Thread.activeCount();i++)
{
if(Thread.currentThread().isAlive())
{
Thread.currentThread().interrupt();
}
}
return super.onClose();
}
My code is.,
new Thread(new Runnable()
{
public void run()
{
loadWebContent(path);
}
}).start();
private void loadWebContent(String path)
{
final VerticalFieldManager vfm = new VerticalFieldManager(HORIZONTAL_SCROLL|VERTICAL_SCROLL)
{
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(maxWidth, (Display.getHeight()-47));
setExtent(maxWidth, (Display.getHeight()-47));
}
};
BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
myBrowserField = new BrowserField(myBrowserFieldConfig);
myBrowserField.addListener(new BrowserFieldListener()
{
public void documentLoaded(BrowserField browserField,Document document) throws Exception
{
UiApplication.getApplication().invokeLater(new Runnable()
{
public void run()
{
try
{
mainlayout.delete(spinner);
mainlayout.add(myBrowserField);
myBrowserField.setZoomScale(1.0f);
}
catch (Exception e)
{
System.out.println("++ "+e);
}
}
});
}
});
myBrowserField.requestContent(path);
}
Pls help how to stop execution when back to first screen.
I think this is Enough:
public class LoadingScreen extends MainScreen
{
ButtonField click;
String url;
BrowserField browserField;
public LoadingScreen()
{
url="http://www.google.com";
browserField=new BrowserField();
add(browserField);
browserField.requestContent(url);
}
}
In onClose method:
public boolean onClose()
{
return super.close();
}
If you have any doubbts come on chat room named "Knowledge sharing center for blackberry and java" to clarify your and our doubts.

Resources