⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contacts.java

📁 java source code for modile list function
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;

public class Contacts extends MIDlet implements CommandListener {
  private Command exitCommand, addCommand, backCommand, saveCommand,
    deleteCommand;
  private Display display;
  private List mainScreen;
  private Form contactScreen;
  private TextField nameField, companyField, phoneField, faxField;
  private ChoiceGroup contactType;
  private boolean editing = false;
  private ContactDB db = null;
  private Vector contactIDs = new Vector();
  private Image[] typeImages;

  public Contacts() {
    // Get the Display object for the MIDlet
    display = Display.getDisplay(this);

    // Create the commands
    exitCommand = new Command("Exit", Command.EXIT, 2);
    addCommand = new Command("Add", Command.SCREEN, 3);
    backCommand = new Command("Back", Command.BACK, 2);
    saveCommand = new Command("Save", Command.OK, 3);
    deleteCommand = new Command("Delete", Command.SCREEN, 3);

    // Create the main screen
    mainScreen = new List("Contacts", List.IMPLICIT);

    // Set the Exit and Add commands for the main screen
    mainScreen.addCommand(exitCommand);
    mainScreen.addCommand(addCommand);
    mainScreen.setCommandListener(this);

    // Create the contact screen
    contactScreen = new Form("Contact Info");
    nameField = new TextField("Name", "", 30, TextField.ANY);
    contactScreen.append(nameField);
    companyField = new TextField("Company", "", 15, TextField.ANY);
    contactScreen.append(companyField);
    phoneField = new TextField("Phone", "", 10, TextField.PHONENUMBER);
    contactScreen.append(phoneField);
    faxField = new TextField("Fax", "", 10, TextField.PHONENUMBER);
    contactScreen.append(faxField);
    String[] choices = { "Personal", "Business", "Family", "Other" };
    contactType = new ChoiceGroup("Type", Choice.EXCLUSIVE, choices, null);
    contactScreen.append(contactType);

    // Set the Back, Save, and Delete commands for the contact screen
    contactScreen.addCommand(backCommand);
    contactScreen.addCommand(saveCommand);
    contactScreen.addCommand(deleteCommand);
    contactScreen.setCommandListener(this);

    // Load the type images
    try {
      typeImages = new Image[4];
      typeImages[0] = Image.createImage("/Personal.png");
      typeImages[1] = Image.createImage("/Business.png");
      typeImages[2] = Image.createImage("/Family.png");
      typeImages[3] = Image.createImage("/Other.png");
    }
    catch (IOException e) {
      System.err.println("EXCEPTION: Failed loading images!");
    }

    // Open the contact database
    try {
      db = new ContactDB("contacts");
    }
    catch(Exception e) {
      System.err.println("EXCEPTION: Problem opening the database.");
    }

    // Read through the database and build a list of record IDs
    RecordEnumeration records = null;
    try {
      records = db.enumerateContactRecords();
      while(records.hasNextElement())
        contactIDs.addElement(new Integer(records.nextRecordId()));
    }
    catch(Exception e) {
      System.err.println("EXCEPTION: Problem reading the contact records.");
    }

    // Read through the database and fill the contact list
    records.reset();
    try {
      while(records.hasNextElement()) {
        Contact contact = new Contact(records.nextRecord());
        mainScreen.append(contact.getName(), typeImages[contact.getType()]);
      }
    }
    catch(Exception e) {
      System.err.println("EXCEPTION: Problem reading the contact records.");
    }
  }

  public void startApp() throws MIDletStateChangeException {
    // Set the current display to the main screen
    display.setCurrent(mainScreen);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
    // Close the contact database
    try {
      db.close();
    }
    catch(Exception e) {
      System.err.println("EXCEPTION: Problem closing the database.");
    }
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
    else if (c == addCommand) {
      // Clear the contact fields
      nameField.setString("");
      companyField.setString("");
      phoneField.setString("");
      faxField.setString("");
      contactType.setSelectedIndex(0, true);

      // Remove the Delete command from the contact screen
      contactScreen.removeCommand(deleteCommand);
      editing = false;

      // Set the current display to the contact screen
      display.setCurrent(contactScreen);
    }
    else if (c == List.SELECT_COMMAND) {
      // Get the record ID of the currently selected contact
      int index = mainScreen.getSelectedIndex();
      int id = ((Integer)contactIDs.elementAt(index)).intValue();

      // Retrieve the contact record from the database
      Contact contact = db.getContactRecord(id);

      // Initialize the contact fields
      nameField.setString(contact.getName());
      companyField.setString(contact.getCompany());
      phoneField.setString(contact.getPhone());
      faxField.setString(contact.getFax());
      contactType.setSelectedIndex(contact.getType(), true);

      // Add the Delete command to the contact screen
      contactScreen.addCommand(deleteCommand);
      editing = true;

      // Set the current display to the contact screen
      display.setCurrent(contactScreen);
    }
    else if (c == deleteCommand) {
      // Get the record ID of the currently selected contact
      int index = mainScreen.getSelectedIndex();
      int id = ((Integer)contactIDs.elementAt(index)).intValue();

      // Delete the contact record
      db.deleteContactRecord(id);
      contactIDs.removeElementAt(index);
      mainScreen.delete(index);

      // Set the current display back to the main screen
      display.setCurrent(mainScreen);
    }
    else if (c == backCommand) {
      // Set the current display back to the main screen
      display.setCurrent(mainScreen);
    }
    else if (c == saveCommand) {
      if (editing) {
        // Get the record ID of the currently selected contact
        int index = mainScreen.getSelectedIndex();
        int id = ((Integer)contactIDs.elementAt(index)).intValue();

        // Create a record for the contact and set it in the database
        Contact contact = new Contact(nameField.getString(), companyField.getString(),
          phoneField.getString(), faxField.getString(), contactType.getSelectedIndex());
        db.setContactRecord(id, contact.pack());
        mainScreen.set(index, contact.getName(), typeImages[contact.getType()]);
      }
      else {
        // Create a record for the contact and add it to the database
        Contact contact = new Contact(nameField.getString(), companyField.getString(),
          phoneField.getString(), faxField.getString(), contactType.getSelectedIndex());
        contactIDs.addElement(new Integer(db.addContactRecord(contact.pack())));
        mainScreen.append(contact.getName(), typeImages[contact.getType()]);
      }

      // Set the current display back to the main screen
      display.setCurrent(mainScreen);
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -