4

Android、J2ME、および に関する多くの質問を読みましRecordStoreたが、まだ満足できる答えが見つかりません。

さまざまなプラットフォームで動作する Java アプリの低レベル部分を実装する必要があります。現在これは Android と J2ME であり、将来的には PC でも動作するはずです。RecordStoreJ2ME の場合とほとんど同じように、単純なデータ セットを保存する必要があります。

アプリはレコードを含む複数のレコード ストアを所有する必要があり、各レコードには次のものがあります。

  • id (ただし、 のように自動返されるものではなく、 「自分のRecordStore」id である必要があります)、
  • データ (単なるバイト配列)。

必要なメソッドを使用してを作成する必要があると思います。Interface各プラットフォームには、 this の独自の実装が必要ですInterface

しかし、このタスクは非常に一般的なようです (少なくとも Android + J2ME では)。車輪の再発明が好きではないという理由だけでお願いしています。

そして多分いくつかの提案はありますか?

4

2 に答える 2

1

そこで、Android 用と J2ME 用の 2 つの実装で、私の要件を満たすインターフェイスを作成しました。

インターフェイスの外観は次のとおりです。

public interface ISDataStore {


   /**
    * Get number of records in data store.
    */
   public int getNumRecords() throws SDataStoreException;

   /**
    * Get size of one record with specified id in bytes.
    *
    * @param record_id id of the record
    */
   public int getRecordSize(int record_id) throws SDataStoreException;

   /**
    * Get record.
    *
    * @param record_id id of the record to read
    * @param data byte array where to put the data
    * @param offset offset in 'data' array from which should start to copy
    */
   public void getRecord(int record_id, byte[] data, int offset) throws SDataStoreException;

   /**
    * Get record.
    *
    * @param record_id id of the record to read
    */
   public byte[] getRecord(int record_id) throws SDataStoreException;

   /**
    * Resolves is record with specified id exists or not.
    *
    * @param record_id id of the record
    * @return true if record exists, otherwise false
    */
   public boolean isRecordExists(int record_id) throws SDataStoreException;

   /**
    * Put new record or update existing one.
    *
    * @param record_id id of the record
    * @param data byte array of data
    * @param offset offset in the data byte array
    * @param length number of bytes to store
    *
    * @return true if operation was successful, otherwise false
    *
    */
   public boolean setRecord(int record_id, byte[] data, int offset, int length) throws SDataStoreException;

   /**
    * Delete the record.
    *
    * @param record_id id of the record
    *
    * @return true if operation was successful, otherwise false
    */
   public boolean deleteRecord(int record_id) throws SDataStoreException;

   /**
    * Clear all the records.
    */
   public void deleteAll() throws SDataStoreException;

   /**
    * Close the data store.
    */
   public void close() throws SDataStoreException;

}

データ ストア用のファクトリもあります。

public interface ISDataStoreFactory {

   /**
    * @param dataStoreId id of the data store
    * @return ISDataStore with given id. Type of this id depends on target platform
    */
   public ISDataStore getDataStore(Object dataStoreId) throws SDataStoreException;

   /**
    * Destroys data store with given id.
    * @param dataStoreId id of the data store. Type of this id depends on target platform
    */
   public void destroyDataStore(Object dataStoreId) throws SDataStoreException;

}

Doxygen によって自動生成されたドキュメントは、ここにあります。

インターフェイスとすべての実装を含む Mercurial リポジトリは、ここにあります

使用方法:

質問で既に述べたように、Android 用のアプリと J2ME 用のアプリがありますが、これらのアプリはどちらも同様のことを行います。(興味のある人は、Bluetooth経由でリモート組み込みデバイスと通信します)

両方のアプリには、主な仕事を行う共通の低レベル部分があります。

私はinterface IMainApp、そのようなものを持っています:

public interface IMainApp {

   public ISDataStoreFactory getDataStoreFactory();

   /*
    * ... some other methods
    */
}

両方のアプリ (Android 用と J2ME 用) には、このインターフェースの独自の実装があり、その参照を低レベル部分に渡します。低レベル部分が何らかのデータ ストアを開きたい場合、ISDataStoreFactory返された を使用しIMainApp.getDataStoreFactoryます。私がやりたいように動作します。

誰にとっても役立つことを願っています。

于 2012-03-26T16:39:49.040 に答える
0

私がやったことはあなたですか

プロファイルを作成します..データベースに保存したい値です

サンプルプロファイル

    public class NewsProfile {

        public String uniqid = "", category = "", title = "" ;

        public NewsProfile(String uniqid) {
            this.uniqid = uniqid;
        }
    }

新しいプロファイルを受け入れるニュースストアを作成する

public void saveProfile(int id, NewsProfile profile) {
    try {
        if (rs != null) {
            profile.id = id;
            byte[] bytes = toByteArray(profile);
            setRecord(profile.id, bytes);
            System.err.println("Exists = " + profile.catKey + String.valueOf(profile.status));
        }
    } catch (Exception e) {
        System.err.println("ERROR: saveUpdateProfile" + e.getMessage());
    }
}

public NewsProfile getProfileint id) throws RecordStoreException, IOException {
    byte[] bytes = rs.getRecord(id);
    DataInputStream is = new DataInputStream(new ByteArrayInputStream(bytes));
    String uniqid = is.readUTF();
    OptionsProfile profile = new NewsProfile (uniqid);
    profile.id = id;
    profile.catKey = uniqid;
    profile.category = is.readUTF();
    profile.title = is.readUTF();
    return profile;
}

private byte[] toByteArray(NewsProfile profile) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream os = new DataOutputStream(baos);
    os.writeUTF(profile.uniqid);
    os.writeUTF(profile.category);
    os.writeUTF(profile.title);
    return baos.toByteArray();
}

これが意味するのは、データベースにデータを保存したいときはいつでも....いつでも保存しているのはNewsProfileです....必要な別のストレージに実装することはできません..SQLite、RMS、さらにはWebサービス

ありがとう :)

于 2012-03-23T09:59:58.997 に答える