private void showMainForm() mainForm = new Form("Facebook 240x320"); mainForm.append("Welcome, User!\n"); mainForm.append("Latest updates:\n\n"); feedList = new List("News Feed", List.IMPLICIT); for (int i = 0; i < authors.size(); i++) String item = authors.elementAt(i) + ":\n" + truncate((String)statuses.elementAt(i), 30) + "\n" + times.elementAt(i); feedList.append(item, null); exitCommand = new Command("Exit", Command.EXIT, 1); postCommand = new Command("Post", Command.SCREEN, 2); refreshCommand = new Command("Refresh", Command.SCREEN, 3); feedList.addCommand(exitCommand); feedList.addCommand(postCommand); feedList.addCommand(refreshCommand); feedList.setCommandListener(this); display.setCurrent(feedList);
private Command exitCommand; private Command postCommand; private Command refreshCommand; private Command selectCommand; private Command backCommand; private Command sendCommand;
public void commandAction(Command c, Displayable d) if (c == exitCommand) notifyDestroyed(); else if (c == postCommand) showPostStatusScreen(); else if (c == refreshCommand) refreshFeed(); else if (c == sendCommand && d == statusBox) String newStatus = statusBox.getString(); postStatus(newStatus); else if (c == backCommand) showMainForm(); else if (c == List.SELECT_COMMAND && d == feedList) int selected = feedList.getSelectedIndex(); if (selected >= 0) showStatusDetails(selected); else if (c == backCommand && d instanceof Form) showMainForm();
public class FacebookMIDlet extends MIDlet implements CommandListener { private Display display; private Form mainForm; private List feedList; private TextBox statusBox; private Alert loadingAlert; facebook java jar 240x320
private void addStatus(String author, String content, String time) authors.addElement(author); statuses.addElement(content); times.addElement(time);
private void postStatus(String message) message.trim().length() == 0) showAlert("Error", "Status cannot be empty!"); return; addStatus("You", message, "Just now"); showAlert("Success", "Status posted to Facebook!"); // Reload feed to show new status refreshFeed();
public FacebookMIDlet() statuses = new Vector(); authors = new Vector(); times = new Vector(); // Sample initial data addStatus("Alex", "Just got back from vacation! 🌊", "10 min ago"); addStatus("Maria", "Coffee and code ☕", "1 hour ago"); addStatus("James", "Anyone going to the concert tonight?", "3 hours ago"); addStatus("Lisa", "New phone, who dis? 😂", "Yesterday"); addStatus("Facebook", "Welcome to Facebook for Java phones!", "2 days ago"); ; import javax
private void showStatusDetails(int index) Form detailForm = new Form("Status Details"); detailForm.append("From: " + authors.elementAt(index) + "\n\n"); detailForm.append(statuses.elementAt(index) + "\n\n"); detailForm.append("Posted: " + times.elementAt(index) + "\n\n"); detailForm.append("[Like] [Comment] [Share]"); backCommand = new Command("Back", Command.BACK, 1); detailForm.addCommand(backCommand); detailForm.setCommandListener(this); display.setCurrent(detailForm);
// FacebookMIDlet.java // A Facebook-style client for 240x320 Java ME phones // Compiles with WTK, runs on J2ME / MIDP 2.0, CLDC 1.1 import javax.microedition.midlet. ; import javax.microedition.lcdui. ; import java.util.*;
private void showAlert(String title, String message) Alert alert = new Alert(title, message, null, AlertType.INFO); alert.setTimeout(2000); display.setCurrent(alert, feedList); private void showAlert(String title
public void startApp() if (display == null) display = Display.getDisplay(this); showMainForm(); else showMainForm();
private void showPostStatusScreen() statusBox = new TextBox("What's on your mind?", "", 200, TextField.ANY); sendCommand = new Command("Send", Command.OK, 1); backCommand = new Command("Back", Command.BACK, 2); statusBox.addCommand(sendCommand); statusBox.addCommand(backCommand); statusBox.setCommandListener(this); display.setCurrent(statusBox);
private Vector statuses; private Vector authors; private Vector times;