2009-07-27 49 views
1

我正在使用黑莓上的電子郵件類應用程序。 我想將所有郵件存儲在持久存儲中。 也需要存儲聯繫人列表。持續存儲處理

如何使用持久存儲操作這種類型的功能?

如何存儲數據?

是否可以保存多條信息?

如何正確使用它們並在屏幕上顯示?

我從來沒有在持久存儲工作過, 請幫忙。

+2

平臺?編程語言? – Extrakun 2009-07-27 07:05:53

+0

黑莓java開發 – iOSDev 2009-07-27 07:08:42

回答

9

永久存儲中沒有索引。
您可以保存和檢索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; 
    }  
} 

郵件持久化:

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

相關問題