سؤال

I am trying to save 10 String values in PersistentStore in my BlackBerry App. The idea is to save the newest 10 items (String values) at any given time. When the first 10 values are saved and the 11th value is typed, it should:

  • Delete the first entry
  • Move the remaining 9 entries above the order
  • Save the 11th entry as the 10th value

This is the logic I want to follow. As the entries keep increasing, I will store maximum 10 entries which would be the latest 10 values. I tried saving the String values through the saveChatMsg() method:

 public void saveChatMsg()
 {
     if(xx<10)
     {
         PersistentStoreHelper.persistentHashtable.put("chatMsg"+xx,chatToSave);
         xx=xx+1;
         if(xx==10)
         {
             PersistentStoreHelper.persistentHashtable.put("xxValue",Integer.toString(0));
         }
         else
         {
             PersistentStoreHelper.persistentHashtable.put("xxValue",Integer.toString(xx));
         }

     }
 }

where xx is an int that goes through 0 upto 9. However, while this is saving the message, when I retrieve the message, it is not displayed in a chronological order. This method is called at 4 different places and so the 10 messages saved are not in the right order; newest message might appear as the 6th value instead of 10 etc. Kindly comment and advice how to implement.

هل كانت مفيدة؟

المحلول

If you want a list of 10 messages, I would use a Vector for that. Vectors have an order to them, and they are Persistable (*). You can remove the first (oldest) element from the Vector, and add a new one to the end.

It looks like your persistent store keeps one main Hashtable (which is good). Change your persistent model to be like this:

- Hashtable
    - Vector (key = "chatMsgs")
        - String
        - String
        - String
        - String
        - String
        - String
        - String
        - String
        - String
        - String

So, maybe something like this:

public void saveChatMsg(String newMsg) {
    Vector msgs = PersistentStoreHelper.persistentHashtable.get("chatMsgs");
    // add the new msg (to the end of the vector)
    msgs.addElement(newMsg);
    // delete old messages, if the vector is full
    while (msgs.size() > 10) {
        msgs.removeElementAt(0);
    }
    // store the modified vector back to the persistent store
    PersistentStoreHelper.persistentHashtable.put("chatMsgs", msgs);
    // I'm assuming your PersistentStoreHelper calls commit() somewhere in here
}

/** @param index - 0 is the oldest, 9 is the newest */
public String getChatMsg(int index) {
    Vector msgs = PersistentStoreHelper.persistentHashtable.get("chatMsgs");
    return (String)msgs.elementAt(index);
}

Edit:

(*) the BlackBerry API doc I linked to, and the BlackBerry Java Storage APIs documentation, both list java.util.Vector as a Persistable class. So does this answer/comment. However, the actual API javadoc for Vector does not say that it implements Persistable. I'm not in a position to run the code right now, but if a Vector of String objects doesn't work for you, you could always use a subclass of Vector, like ContentProtectedVector, that the API docs explicitly list as Persistable. Post a comment if you wind up needing to do that, for others' benefit.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top