【问题标题】:How to sort Persistent store records in Blackberry application?如何在黑莓应用程序中对持久存储记录进行排序?
【发布时间】:2014-11-15 17:43:06
【问题描述】:

您能帮我对黑莓应用程序中的持久存储记录进行排序吗? 我想按升序和降序排序...

【问题讨论】:

    标签: blackberry java-me persistent-storage


    【解决方案1】:

    对于黑莓中的排序,您可以使用 SimpleSortingVector 但坏消息是,SimpleSortingVector 是不可持久的。 因此,要在持久存储中进行排序,您必须编写 你自己的排序方法。

    Vector info = (Vector) store.getContents();
    

    现在可以使用任何排序算法对向量的内容进行排序。

    【讨论】:

      【解决方案2】:

      我假设您指的是存储在 PersistentStore 中的排序记录,而不仅仅是实现 Persistable 接口的对象。

      为了将您的数据提交到 PersistentStore:

      SimpleSortingVector ssv = new SimpleSortingVector();
      // Construct your records and add them to the vector
      
      PersistentObject po = PersistentStore.getPersistentObject(persistentStoreKey);
      po.setContents(new ControlledAccess(ssv, codeSigningKey));
      po.commit();
      

      为了检索向量:

      Object contents = po.getContents();
      if (contents instanceof SimpleSortingVector)
      {
          // Cast to a SimpleSortingVector and load your data
      }
      

      我相信将 SimpleSortingVector 放入 PersistentStore 应该没有任何问题。但是,它确实融合了数据存储与其行为之间的界限(因为您不仅要存储数据记录,还要存储比较器和排序信息)。将记录作为 Vector 存储在 PersistentStore 中可能是一种更简洁的解决方案,当您从 PersistentStore 加载它时,将其复制到您在应用程序运行时使用的 SimpleSortingVector 中。如果您采用这种方法,您可以将有关 Comparator 的信息和排序信息以指定格式存储在 Vector 本身中,并在构造 SimpleSortingVector 时将其提取出来。

      【讨论】:

      • 嗨,我想对持久存储记录进行排序,通过实现类似“Select TimeStamp from Table where ID = + someID order by Date desc limit 0,1”之类的查询我如何在存储中订购记录和选择最晚的日期?
      • 您可以将所有记录放在一个 SimpleSortingVector 中,并实现一个比较器来使用它。 SimpleSortingVector 将使用比较器的 compare(Object o1, Object o2) 方法对向量进行排序(您可以按日期排序)。要实现更复杂的类似 SQL 的查询,您可能需要更强大的功能。
      猜你喜欢
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2013-02-28
      • 2012-01-30
      相关资源
      最近更新 更多