Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;
import java.lang.*;
public class ChoiceGroupOptionActionTest7 extends MIDlet implements CommandListener, ItemStateListener, ItemCommandListener {
private RecordStore defaultChoicesTestDb = null;
private RecordStore userChoicesTestDb = null;
private RecordStore sortedUserChoicesTestDb = null;
private Command exitCommand, goCommand, saveCommand, saveNewChoiceCommand, cancelCommand, addAChoiceCommand;
private Display display;
private Form screen;
private Form frmAddChoice;
private Form frmDisplaySavedInfo;
private Alert alert;
private int i, intNumberOfRecords, intSelectedIndex, intSelectedChoice;
private int intNumberOfDefaultRecords, intNumberOfUserRecords, intNumberOfSortedUserRecords;
private ChoiceGroup cgChoices;
private TextField sampleTextField;
private String strSelectedIndex, strSelectedChoice, strSelectedChoiceForStateChange;
private Comparator comparator = null;
public ChoiceGroupOptionActionTest7() {
// Get the display object for the MIDlet.
display = Display.getDisplay(this);
// Create the Exit and Go commands.
exitCommand = new Command("Exit", Command.EXIT, 2);
goCommand = new Command("Go", Command.OK, 2);
saveCommand = new Command("Save", Command.OK, 2);
saveNewChoiceCommand = new Command("Save", Command.OK, 2);
cancelCommand = new Command("Cancel", Command.CANCEL, 2);
addAChoiceCommand = new Command("Add", Command.OK, 2);
// Create the screen form.
screen = new Form("Main Screen");
// Set the Exit and Add commands for the screen.
screen.addCommand(exitCommand);
screen.setCommandListener(this);
screen.setItemStateListener(this);
}
//public void startApp() throws MIDletStateChangeException {
public void startApp() {
// Create a text field for the main screen.
sampleTextField = new TextField("This is a sample text field", "", 40, TextField.ANY);
sampleTextField.addCommand(addAChoiceCommand); // In the beginning, this displays an "Add" button when the display is selected.
sampleTextField.setItemCommandListener(this);
screen.append(sampleTextField);
// Open the default choices record store.
try {
defaultChoicesTestDb = RecordStore.openRecordStore("defaultchoicestestfile7", true);
} catch (RecordStoreNotFoundException rsnfe) {
// Handle the exception.
} catch (RecordStoreFullException fsfe) {
// Handle the exception.
} catch (RecordStoreException rse) {
// Handle the exception.
}
// Open the user choices record store.
try {
userChoicesTestDb = RecordStore.openRecordStore("userchoicestestfile7", true);
} catch (RecordStoreNotFoundException rsnfe) {
// Handle the exception.
} catch (RecordStoreFullException fsfe) {
// Handle the exception.
} catch (RecordStoreException rse) {
// Handle the exception.
}
// Open the sorted user choices record store.
try {
sortedUserChoicesTestDb = RecordStore.openRecordStore("sorteduserchoicestestfile7", true);
} catch (RecordStoreNotFoundException rsnfe) {
// Handle the exception.
} catch (RecordStoreFullException fsfe) {
// Handle the exception.
} catch (RecordStoreException rse) {
// Handle the exception.
}
// If the default choices record store has no records yet, write to it.
try {
intNumberOfRecords = defaultChoicesTestDb.getNumRecords();
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
if (intNumberOfRecords == 0) {
writeRecordToDefaultStore("Choice 1");
writeRecordToDefaultStore("Choice 2");
writeRecordToDefaultStore("Choice 3");
}
String[] strDefaultChoices = { "(Add a choice)" };
cgChoices = new ChoiceGroup("Choices:", List.EXCLUSIVE, strDefaultChoices, null);
cgChoices.addCommand(addAChoiceCommand);
cgChoices.setItemCommandListener(this);
// Read from the default choices record store and write to the user choices record store.
String strTheChoice = null;
try{
if (defaultChoicesTestDb.getNumRecords() > 0) {
RecordEnumeration reDefaultChoicesEnumeration = defaultChoicesTestDb.enumerateRecords(null, null, false);
try {
intNumberOfUserRecords = userChoicesTestDb.getNumRecords();
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
// If the user choices record store does not yet have the default choices, write them to it.
if (intNumberOfUserRecords == 0) {
while (reDefaultChoicesEnumeration.hasNextElement()) {
try {
strTheChoice = new String(reDefaultChoicesEnumeration.nextRecord());
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
writeRecordToUserStore(strTheChoice);
}
}
}
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
// Read from the user choices record store.
String strTheUserChoice = null;
try{
if (userChoicesTestDb.getNumRecords() > 0) {
RecordEnumeration reUserChoicesEnumeration = userChoicesTestDb.enumerateRecords(null, null, false);
while (reUserChoicesEnumeration.hasNextElement()) {
try {
strTheUserChoice = new String(reUserChoicesEnumeration.nextRecord());
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
System.out.println("------------------------------");
System.out.println("The user choice: " + strTheUserChoice);
System.out.println("------------------------------");
}
}
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
// Close the sorted user record store, delete it, and open it again
// before writing the contents of the user record store to it.
// Close the sorted user choices record store first.
try {
sortedUserChoicesTestDb.closeRecordStore();
}
catch (Exception error) {
alert = new Alert("Error Closing",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
// Then delete the sorted user record store.
try {
sortedUserChoicesTestDb.deleteRecordStore("sorteduserchoicestestfile7");
} catch (RecordStoreNotFoundException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
// Then open the sorted user record store again.
try {
sortedUserChoicesTestDb = RecordStore.openRecordStore("sorteduserchoicestestfile7", true);
} catch (RecordStoreNotFoundException rsnfe) {
// Handle the exception.
} catch (RecordStoreFullException fsfe) {
// Handle the exception.
} catch (RecordStoreException rse) {
// Handle the exception.
}
// Read from the user choices record store and write to the sorted user choices record store.
try{
if (userChoicesTestDb.getNumRecords() > 0) {
Comparator comp = new Comparator();
RecordEnumeration reUserChoicesEnumeration = userChoicesTestDb.enumerateRecords(null, comp, false);
try {
intNumberOfSortedUserRecords = sortedUserChoicesTestDb.getNumRecords();
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
// If the sorted user choices record store does not yet have the user choices, write them to it.
if (intNumberOfSortedUserRecords == 0) {
while (reUserChoicesEnumeration.hasNextElement()) {
//String strTheChoice = new String(reUserChoicesEnumeration.nextRecord());
try {
strTheChoice = new String(reUserChoicesEnumeration.nextRecord());
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
writeRecordToSortedUserStore(strTheChoice);
}
}
}
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
// Read from the sorted user choices record store and display to the screen.
String strTheSortedUserChoice = null;
try{
if (sortedUserChoicesTestDb.getNumRecords() > 0) {
RecordEnumeration reSortedUserChoicesTestEnumeration = sortedUserChoicesTestDb.enumerateRecords(null, null, false);
while (reSortedUserChoicesTestEnumeration.hasNextElement()) {
try {
strTheSortedUserChoice = new String(reSortedUserChoicesTestEnumeration.nextRecord());
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
cgChoices.append(strTheSortedUserChoice, null);
System.out.println("------------------------------");
System.out.println("The sorted user choice: " + strTheSortedUserChoice);
System.out.println("------------------------------");
}
}
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
screen.append(cgChoices);
// Make the first item in the choice group be selected.
cgChoices.setSelectedIndex(0, true);
intSelectedChoice = cgChoices.getSelectedIndex();
strSelectedIndex = Integer.toString(intSelectedChoice);
strSelectedChoice = cgChoices.getString(intSelectedChoice);
// Set the current display to the screen.
display.setCurrent(screen);