我正在Blackberry上开发电子邮件类应用程序。 我想将所有邮件存储在持久存储中。 此外,还需要存储联系人列表。
如何使用persistet store操作这种功能?
如何存储数据?
可以保存多条消息吗?
如何正确地控制它们并在屏幕上显示?
我以前从未在持久商店工作过, 请帮忙。
答案 0 :(得分:9)
持久存储中没有索引 您可以保存和检索Persistable对象的数组 例如,Contact Persistable:
public class Contact implements Persistable{
int mId; String mAdress; String mName;
public Contact( int id, String adress, String name ){
mId = id; mAdress = adress; mName = name;
}
}
Mail Persistable:
public class Mail implements Persistable{
String mMessage = null; int mSenderId = -1; int[] mReceiverIdList = null;
public Mail(String message, int senderId, int[] receiverIdList){
mMessage = message; mSenderId = senderId;
mReceiverIdList = receiverIdList;
}
}
一些助手类:
public class PersistentStoreHelper{
static PersistentObject contactStore = PersistentStore
.getPersistentObject( 0xf140775afcb94f90L );
static PersistentObject mailStore = PersistentStore
.getPersistentObject( 0xd43b0423228ff7c0L );
public static void saveContacts( Contact[] contacts ){
saveObject( contactStore, contacts );
}
public static void saveMails( Mail[] mails ){
saveObject( mailStore, mails );
}
public static Contact[] retrieveContacts(){
return ( Contact[] )retrieveObject( contactStore );
}
public static Mail[] retrieveMails(){
return ( Mail[] )retrieveObject( mailStore );
}
public static void saveObject( PersistentObject store, Object object ){
synchronized( store ){
store.setContents( object );
store.commit();
}
}
public static Object retrieveObject( PersistentObject store ){
Object result = null;
synchronized( store ){
result = store.getContents();
}
return result;
}
}
使用样本:
class Scr extends MainScreen implements FieldChangeListener{
ButtonField mBtnInit = null;
BasicEditField mInputSenderId = null;
BasicEditField mInputReceiverId = null;
ButtonField mBtnSearch = null;
VerticalFieldManager mMailsList = null;
public Scr(){
mBtnInit = new ButtonField( "Init Persistenet Storage",
ButtonField.CONSUME_CLICK );
mBtnInit.setChangeListener( this );
add( mBtnInit );
mInputSenderId = new BasicEditField( "sender id:", "43" );
add( mInputSenderId );
mInputReceiverId = new BasicEditField( "receiver id:", "12" );
add( mInputReceiverId );
mBtnSearch = new ButtonField( "Search", ButtonField.CONSUME_CLICK );
mBtnSearch.setChangeListener( this );
add( mBtnSearch );
mMailsList = new VerticalFieldManager();
add( mMailsList );
}
public Vector getMailByIds( int senderId, int recepientId ){
Vector result = new Vector();
Mail[] mails = PersistentStoreHelper.retrieveMails();
for( int i = 0; i < mails.length; i++ )
if( mails[ i ].mSenderId == senderId ){
int[] receiverIdList = mails[ i ].mReceiverIdList;
for( int j = 0; j < receiverIdList.length; j++ )
if( recepientId == receiverIdList[ j ] )
result.addElement( mails[ i ] );
}
return result;
}
public void initPersistentStorage(){
// create 100 contacts and save them
Contact[] contacts = new Contact[ 100 ];
for( int i = 0; i < 100; i++ ){
String name = "name" + String.valueOf( i );
String adress = name + "@mail.com";
contacts[ i ] = new Contact( i, adress, name );
}
PersistentStoreHelper.saveContacts( contacts );
// create messages from each to every contact and save them
Mail[] mails = new Mail[ 10000 - 100 ];
int k = 0;
for( int i = 0; i < 100; i++ )
for( int j = 0; j < 100; j++ )
if( i != j ){
Mail mail = new Mail( "Hello!", contacts[ i ].mId,
new int[]{ contacts[ j ].mId } );
mails[ k ] = mail;
k++;
}
PersistentStoreHelper.saveMails( mails );
}
public void fieldChanged( Field field, int context ){
if( field == mBtnInit )
initPersistentStorage();
else if( field == mBtnSearch ){
mMailsList.deleteAll();
int senderId = Integer.parseInt( mInputSenderId.getText() );
int receiverId = Integer.parseInt( mInputReceiverId.getText() );
Contact[] contacts = PersistentStoreHelper.retrieveContacts();
Vector result = getMailByIds( senderId, receiverId );
for( int i = 0, cnt = result.size(); i < cnt; i++ )
{
Mail mail = ( Mail )result.elementAt( i );
String from = "From: " + contacts[ mail.mSenderId ].mName
+ " <" + contacts[ mail.mSenderId ].mAdress + ">";
String to = "To: ";
for( int j = 0; j < mail.mReceiverIdList.length; j++ )
{
int id = mail.mReceiverIdList[ j ];
to += contacts[ id ].mName + " <"
+ contacts[ id ].mAdress + ">; ";
}
to = to.substring( 0, to.length() - 2 );
String msg = "Message: " + mail.mMessage;
mMailsList.add( new LabelField( from ) );
mMailsList.add( new LabelField( to ) );
mMailsList.add( new LabelField( msg ) );
}
}
}
}
阅读BlackBerry Java Application - Core - Development Guide - Persistent Storage
您也可以阅读riccomini - code blackberry persistent store