Troubleshooting guide

71
3: Storing data
Code sample: Storing and viewing restaurant information
This code sample lets BlackBerry® device users save information about multiple restaurants and view information
about the most recently saved restaurant.
Example: Restaurants.java
/**
* Restaurants.java
* Copyright (C) 2004-2005 Research In Motion Limited.
*/
package com.rim.samples.docs.restaurants;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.util.*;
import java.util.*;
import net.rim.device.api.i18n.*;
import net.rim.blackberry.api.invoke.*;
import net.rim.blackberry.api.browser.*;
import com.rim.samples.docs.resource.*;
public class Restaurants extends UiApplication implements RestaurantResource {
private AutoTextEditField namefield;
private AutoTextEditField addressfield;
private EditField phonefield;
private EditField websitefield;
private EditField specialtyfield;
private static Vector _data;
private static PersistentObject store;
private static ResourceBundle _resources;
private MenuItem saveItem = new MenuItem(_resources.getString(MENUITEM_SAVE), 110, 10)
{
Retrieve the most recently
saved object.
>Invoke _data.lastElement().
public void run() {
synchronized(store) {
_data = (Vector)store.getContents();
if (!_data.isEmpty()) {
RestaurantInfo info = (RestaurantInfo)_data.lastElement();
namefield.setText(info.getElement(RestaurantInfo.NAME));
addressfield.setText(info.getElement(RestaurantInfo.ADDRESS));
phonefield.setText(info.getElement(RestaurantInfo.PHONE));
specialtyfield.setText(info.getElement(
RestaurantInfo.SPECIALTY));}
}
}
Task Steps