Troubleshooting guide

67
3: Storing data
Code sample: Saving user name and password information
This code sample demonstrates how to create an application for BlackBerry® device users that lets them view
their current user names and passwords, type new user names and passwords, and save changes.
Example: UserInfo.java
/**
* UserInfo.java
* Copyright (C) 2001-2005 Research In Motion Limited. All rights reserved.
*/
package com.rim.samples.docs.userinfo;
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 com.rim.samples.docs.resource.*;
public class UserInfo extends UiApplication implements UserInfoResource
{
private static PersistentObject store;
private static ResourceBundle _resources;
private AutoTextEditField usernamefield;
private PasswordEditField passwordfield;
Retrieve persistent data. 1. Invoke getContents() on a PersistentObject.
2. To convert the object that PersistentObject.getContents() returns to a specific object type,
perform an explicit cast on the object that
PersistentObject.getContents() returns.
synchronized(store) {
String[] currentinfo = (String[])store.getContents();
if(currentinfo == null) {
Dialog.alert(_resources.getString(APP_ERROR));
}
else {
currentusernamefield.setText(currentinfo[0]);
currentpasswordfield.setText(currentinfo[1]);
}}
Delete all persistent data
from an application.
If you delete the .cod file that defines a PersistentStore, then all persistent objects that the .cod file
created are deleted.
>Invoke PersistentStore.destroyPersistentObject(), providing as a parameter a unique key
for the
PersistentObject.
Delete specific persistent
data from an application.
> To delete individual data, treat the data as normal object, and delete references to it. A garbage collected
operation delete the data.
Task Steps