Java ME - Multiple forms, moving from one screen to the next - java-me

I am using Java Micro Edition and I am trying to create a simple login form with a record store. When the user enters the details I'd like to check them against the ones stored and then move onto another screen like a welcome area.
I have a feeling it has something to do with the form element and switching between it but I can't seem to get anywhere with google

try this
form = new Form("login");
form.addCommand(getExitCommand());
form.addCommand(getOkCommand());
form.setCommandListener(this);
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
exitMIDlet();
} else if (command == okCommand) {
display.setCurrent(getWelcomeForm());
}
} else if (displayable == form1) {
if (command == backCommand) {
// do something else
}
}
}

display is something that should be created in the constructor and also above it i.e.
public class YourMidlet extends MIDlet implements CommandListener {
private Display display;
private Form form1;
private Form form2;
public YourMidlet
{
display = Display.getDisplay(this);
form 1 = new Form("hello form this is form 1");
form 2 = new Form("hello form 2");
display.setCurrent(form1);
}
}
you then do:
display.setCurrent(form2);
to switch to form 2

Related

MVVMCross Data Binding after returning from ViewController

I am using MVVMCross 3.2.2 as part of an iOS/Android app. One of my screens has multiple views that are displayed depending upon the selection in a Tab bar like row of buttons. Different data is displayed in each of these these views individual UITableView. The data binding works perfectly.
I also have a menu, that has a "profile" selection. Changing the profile fires an MvxMessage that my HomeView receives and then uses the message to set the ViewModel up to filter the data to be displayed. This all seems to work perfectly and the data is displayed correctly.
If I do something in the HomeView that displays another view using ShowViewModel(). When I return back to the home view the binding no longer works properly when a profile changes is made. The message gets handled, the data gets filtered, but a call to ReloadDataTable on the UITableView does not change the data.
ViewModel
#region Groupings
public IList<Group> Groups{
get { return _groupService.GetAll(); }
}
public void SetupSubGroups(Group group)
{
if (group == null)
{
_groups = new ObservableCollection<Group> ();
if (_profileService.SelectedProfile != null)
{
var grp = _groupService.GetByGroupName (_profileService.SelectedProfile.Name);
if (grp == null)
grp = new Group { Name = _profileService.SelectedProfile.Name };
_groups.Add (grp);
}
}
else
{
var litsOfGroups = _groupService.GetSubGroups (group);
foreach (var grp in litsOfGroups)
_groups.Add (grp);
}
RaisePropertyChanged(() => AvailableGroups);
}
private ObservableCollection <Group> _groups;
public ObservableCollection<Group> AvailableGroups {
get { return _groups; }
}
#endregion
ViewController
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var groupSource = new GroupTableViewDataSource (TableViewGroups);
TableViewGroups.Source = groupSource;
_localViewModel.SetupSubGroups (null);
_bindingSet = this.CreateBindingSet<HomeViewController, HomeViewModel> ();
_bindingSet.Bind (groupSource).To (vm => vm.AvailableGroups);
_bindingSet.Apply ();
TableViewReportTags.ReloadData ();
NavigationController.NavigationBarHidden = false;
}
private void OnProfileChanged(ProfileChangedMessage message)
{
_localViewModel.SetupSubGroups (null);
TableViewGroups.ReloadData ();
}
private HomeViewModel _localViewModel { get { return ViewModel as HomeViewModel; } }
Any ideas what I can look at, or change would be really useful. I have spend many hours on this, and have made no progress.

How to do per-keystroke validation of TextFieldTableCell?

I am trying to do per-keystroke validation in a JavaFX TextFieldTableCell but I do not know how to capture the text-changed events from the embedded TextField control.
If the object in question were simply a TextField, then textField.textProperty().addListener(myChangeListener) would do the trick. TextFieldTableCell also exposes textProperty(), but this property behaves quite differently on TextFieldTableCell. It does not generate change events on a per-keystroke basis. Rather, I see lots of events when the TableView is first displayed, and I see one event each time I begin editing in a cell.
First of all, about textProperty().
Look here to see :
http://docs.oracle.com/javafx/2/api/index.html
TextProperty() is a property of labeled parent class, you will learn nothing from it, because it is not used. It is tricky thing : cell - inheritant of labeled control. TextField, which you see, when start editing, it is a graphic node of cell (graphicProperty()) (as far as I remember documentation).
And, this graphic node is assigned by a text field, only when editing starts.
AFAIK, there is no direct access to editable node.
The way to solve the issue - implement editable cell by your self.
Let me talk to developer, to learn more...
Supposing, you have DataItem class, which contains String, and supposing that TableView has encapsulated data type DataItem, and the only column has the same encapsulated data type, you may use this implementation as basis :
public class TextFieldTableCell extends TableCell<DataItem, DataItem> {
private TextField textField;
public TextFieldTableCell() {
}
#Override
public void startEdit() {
super.startEdit();
if (isEmpty()) {
return;
}
if (textField == null) {
createTextBox();
} else {
textField.setText(new CellCustomStringConverter().toString(getItem()));
}
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
textField.requestFocus();
textField.selectAll();
}
#Override
public void cancelEdit() {
super.cancelEdit();
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
#Override
public void updateItem(DataItem item, boolean empty) {
super.updateItem(item, empty);
if (!isEmpty()) {
if (textField != null) {
textField.setText(new CellCustomStringConverter().toString(item));
}
setText(item.toString());
}
}
private void createTextBox() {
textField = new TextField(new CellCustomStringConverter().toString(getItem()));
textField.setId(TABLE_EDIT_ID);
textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
commitEdit(new DataItem(textField.getText()));
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
});
}
}

In J2ME, is that possible to operate an Alert dialog box with Yes & NO command?

I have created an Alert dialog box in my J2ME app to alert user when user press exit button to terminate an app and ask user confirmation to exit from app with yes and no command.
When user press Yes button app will terminate and when user press No button app will return to its Main form. To do this I developed a code from scratch which are as follows:
public class CustomAlert extends MIDlet implements CommandListener
{
Alert ExitAlrt;
Display d;
Command MainListSelect, Exit, YesCmdAlrt, NoCmdAlrt;
List MainList;
public CustomAlert()
{
d = Display.getDisplay(this);
//Initialization of commands
MainListSelect = new Command("Select", Command.SCREEN, 1);
Exit = new Command("Exit", Command.STOP, 2);
//Initialization of lists
MainList = new List("Menu", List.IMPLICIT);
//Adding command to lists
MainList.addCommand(MainListSelect);
MainList.addCommand(Exit);
MainList.setCommandListener(this);
//Appending the content of lists
MainList.append("Settings",null);
}
protected void startApp()
{
MainList.setSelectedIndex(0, true);
d.setCurrent(MainList);
}
protected void pauseApp() { }
protected void destroyApp(boolean unconditional){}
//This method handle commands which operate list that is Select & Exit
public void commandAction(Command cmd,Displayable dispable)
{
if(cmd == MainListSelect)
{
int slctindx = MainList.getSelectedIndex();
if(slctindx == 0)
{}
else if(slctindx == 1)
{}
}
if(cmd == Exit)
{
ExitAlrt = new Alert("Application Alert","Are you sure you want to exit?",null, AlertType.WARNING);
YesCmdAlrt = new Command("Yes", Command.EXIT,1);
ExitAlrt.addCommand(YesCmdAlrt);
NoCmdAlrt = new Command("No", Command.SCREEN,2);
ExitAlrt.addCommand(NoCmdAlrt);
d.setCurrent(ExitAlrt);
}
}
//This Code handle Commands present on Alert dialog box.
public void commandAction(Command cmd) /
{
ExitAlrt.setCommandListener(this);
if(cmd == NoCmdAlrt)
{
d.setCurrent(MainList);
}
else if(cmd == YesCmdAlrt)
{
destroyApp(true);
notifyDestroyed();
}
}
}
In above code problem is when I click on Exit button, Alert box appears and when I press Yes button to terminate an app it again redirect to me on Main List of an app. I did lot of placements in code but problem remain constant.
What is solution for this?
ExitAlert in the posted code snippet lacks a command listener because you didn't invoke setcommandListener for it. As a result, instead of expected exit, default command action happens which is to simply dismiss the alert, as explained in API javadocs:
If the user invokes a Command and the default listener is present, the default listener ignores the Command and implements the automatic-advance behavior.
Note you might think that ExitAlrt.setCommandListener(this) inside commandAction(Command cmd) method does the trick for you but this is not so, because this method is not invoked in between creation of the ExitAlrt instance and displaying it.
To get the desired behavior, implement and set an appropriate command listener for ExitAlrt prior to invoking setCurrent.
// ...
if(cmd == Exit)
{
System.out.println("Exit command invoked"); // log message for debugging
Alert ExitAlrt = new Alert("Application Alert",
"Are you sure you want to exit?", null, AlertType.WARNING);
ExitAlrt.addCommand(new Command("Yes", Command.EXIT, 1));
ExitAlrt.addCommand(new Command("No", Command.SCREEN, 2));
// --> set command listener for ExitAlrt prior to invoking setCurrent
ExitAlrt.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
System.out.println("command: [" + c.getCommandLabel()
+ "] at screen: [" + d.getTitle() + "]"); // for debugging
if (c.getCommandType() != Command.EXIT) {
System.out.println("Exit cancelled"); // for debugging
d.setCurrent(MainList);
return;
}
System.out.println("Exit confirmed"); // for debugging
destroyApp(true);
notifyDestroyed();
}
});
d.setCurrent(ExitAlrt);
}
// ...
For simplicity, above code snippet uses System.out.println for logging. If needed, refer to another SO question for an explanation and example of how this could be done in a more practical way.

Preventing TabControl selection in Silverlight

Is there any way to prevent the change of a tab in TabControl in Silverlight 4?
A simple case is when I've got a form with some data, and I want to ask the user if he/she wants to save this data before actually changing the tab.
I've seen code examples on how to do this in WPF, but not in Silverlight.
What can I do to stop the tab from changing?
Bind SelectedIndex to a property on your data context.
<sdk:TabControl SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}">
<sdk:TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</sdk:TabItem>
<sdk:TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</sdk:TabItem>
</sdk:TabControl>
In the SET accessor, write your code to check whether the user really wants to do what they're trying to do.
public class Context : INotifyPropertyChanged
{
int _SelectedIndex = 0;
public int SelectedIndex
{
get
{
return _SelectedIndex;
}
set
{
MessageBoxResult result = MessageBox.Show("Do you want to save?", "Really?", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
_SelectedIndex = value;
}
RaisePropertyChanged("SelectedIndex");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
Net effect is, if the user chooses 'cancel' on the dialogue the private variable is never changed - the PropertyChanged event fires, rebinding the selected index to the existing value.
Hope this is what you were trying to accomplish.
UPDATE (11/10/2012) - Alternate method (possibly for SL5?). Write code behind to tie into SelectionChanged event of your TabControl, reset the tab control's selected item property based on your test.
private void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0)
{
MessageBoxResult result = MessageBox.Show("Do you want to save?", "Really?", MessageBoxButton.OKCancel);
if (result != MessageBoxResult.OK)
{
((TabControl)sender).SelectionChanged -= new SelectionChangedEventHandler(TabControl_SelectionChanged);
((TabControl)sender).SelectedItem = e.RemovedItems[0];
((TabControl)sender).SelectionChanged += new SelectionChangedEventHandler(TabControl_SelectionChanged);
}
}
}

Change display in form to another form in j2me

I get trouble when I created one form and one midlet and I want through an alert for user when user enters detail, but I got trouble it can't change screen to show alert
nothing can be easier with the code provided in rev 4 of your question
// ... your AddReview class
public AddReview(String title, MainFoodie mf) {
super(title);
this.mf = mf; // without that you'll get NPE in changeSreen
// ...
}
//...
private void changeScreen(Alert a, Form f){
Display.getDisplay(mf).setCurrent(a, f);
}
public void commandAction(Command c, Displayable d) {
if(c.getLabel().equals("Save")){
// how to show alert here:
changeScreen(cfmAlert, this)
}

Resources