1

I have a main class and a screen class. I have a button that launches a datepicker. When the datepicker is closed I need the label of the button to be refreshed with the selected value. How should I do that?

I tried with invalidate, creating a CustomButtonField that implements different onFocus, onUnFocus, and some other stuff but I guess I implemented them in a wrong way...

My two clases are these ones...

Main...

public class TestClass extends UiApplication
{
    public static Calendar alarm1time = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));

    public static void main(String[] args)
    {
        TestClass testClass = new TestClass (); 
        testClass.enterEventDispatcher();
    }

    public TestClass()
    {
        pushScreen(new TestClassScreen());
    }
}

Screen...

public final class TestClassScreen extends MainScreen
{
    public TestClassScreen()
    {
        ButtonField alarm1 = new ButtonField("Alarm : " + TestClass.alarm1time.get(Calendar.HOUR_OF_DAY) + ":" + TestClass.alarm1time.get(Calendar.MINUTE) ,ButtonField.FOCUSABLE)
        {
            public boolean navigationClick (int status , int time)
            {
                datePicker(1);
                return true;
            }
        };
        setTitle("Test Alarm");
        add(new RichTextField(" "));
        add(alarm1 );
    }

    public void datePicker(final int alarmNumber)
    {
        UiApplication.getUiApplication().invokeLater(new Runnable()
        {
            public void run()
            {
                DateTimePicker datePicker = null;
                datePicker = DateTimePicker.createInstance(TestClass.alarm1time, null, "HH:mm");
                if(datePicker.doModal())
                {
                    Calendar cal = datePicker.getDateTime();
                    TestClass.alarm1time = cal;
                    //Here I need the label to be refreshed, after the datePicker is Ok
                }
            }
        });
    }
}
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44

2 Answers2

0

I found one way in the Blackberry Development Forum...

public boolean navigationClick (int status , int time)
{
    datePicker(1, this);
    return true;
}

public void datePicker(final int alarmNumber, final ButtonField buttonToUpdate)
{
    UiApplication.getUiApplication().invokeLater(new Runnable()
    {
        public void run()
        {
            DateTimePicker datePicker = null;
            datePicker = DateTimePicker.createInstance(TestClass.alarm1time, null, "HH:mm");
            if(datePicker.doModal())
            {
                Calendar cal = datePicker.getDateTime();
                TestClass.alarm1time = cal;
                buttonToUpdate.setLabel(....)
            }
        }
    });
}

A more usual way would be to have change listener listen for Button Clicks and do similar processing in there.

0

I think, this helps you:

public class Def extends MainScreen
{   
ButtonField show;
LabelField label;
String str="";
ObjectChoiceField choiceField;
public Def() 
{
    createGUI();
}

private void createGUI()
{   
    String st_ar[]={"Date Picker"};
    choiceField=new ObjectChoiceField("Select Date: ", st_ar)
    {
        protected boolean navigationClick(int status, int time) 
        {
            DateTimePicker datePicker = DateTimePicker.createInstance( Calendar.getInstance(), "yyyy-MM-dd", null);
            datePicker.doModal();
            Calendar calendar=datePicker.getDateTime();
            str=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.YEAR);
            return true;
        }
    };
    add(choiceField);       

    label=new LabelField("",Field.NON_FOCUSABLE);
    add(label);

    show=new ButtonField("Show");
    show.setChangeListener(new FieldChangeListener() 
    {
        public void fieldChanged(Field field, int context) 
        {
            label.setText("Time is: "+str);
        }
    });
    add(show);
}
}
alishaik786
  • 3,696
  • 2
  • 17
  • 25