2012-03-22 3 views
4

Android、J2ME、RecordStoreに関する多くの質問を読んだことがありますが、まだ私の満足できる答えは見つかりません。Android、J2ME、PC用の一般的なシンプルなデータストレージインターフェイス

私はJavaアプリケーションの低レベル部分を実装する必要があります。これは異なるプラットフォームで動作するはずです。今はAndroidとJ2MEですが、将来はPCでも動作するはずです。私は、単純なデータセットを格納する必要があり、それは、J2MEでRecordStoreとほぼ同様である。

アプリケーションはレコードを持つ複数のレコード店を所有する必要があり、各レコードは持っている:

  • idが(それは "する必要があります私の "id、RecordStoreのように自動返されません)、
  • データ(単なるバイト配列)です。

私は必要な方法でInterfaceを書くべきだと思います。それぞれのプラットフォームには、このInterfaceの独自の実装が必要です。

しかし、このタスクは非常に一般的なようです(少なくとも、Android + J2MEのため)、おそらく既に軽量実装がいくつかありますか?私は車輪を再発明するのが好きではないからと言っています。

おそらくいくつかの提案はありますか?

私は

iはデータベースに格納する値です。..プロファイルを作成して何をしたか

+0

私はRecordStoreがAndroidでどれくらいうまく動作しているかわからない...あなたの書き込みたい場合はヒントを教えてください – Baba

+0

Androidには「RecordStore」はまったくありません。誰かにヒントを与えることができれば、それを与えることができるのではなく、すぐにそれを伝えてみましょう。 =) –

答えて

1

..あなたが望む別のストレージのためのSQLite、RMS、でもWebサービスを実装することはできません私は、AndroidとJ2MEの2つの実装で自分の要件を満たすインターフェイスを作成しました。

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のではhereを見つけることができます:

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; 

} 

データストアの工場もあります。ここでは

は、インターフェイスの外観をどうするかです。

インタフェースを持つMercurialリポジトリとすべての実装はhereです。

どのように私はそれを使うのです:私はすでに私の質問で述べたよう

、私は、J2MEのためのAndroidアプリのためのアプリを持って、両方のこれらのアプリケーションは、同様のことを行います。(誰でも興味があれば、リモート内蔵デバイスとのブルートゥース経由の通信を行います)

両方のアプリケーションには、メインジョブを実行する一般的な低レベルの部分があります。

私はそのようなinterface IMainApp、何かを持っている:(Android用およびJ2ME用)

public interface IMainApp { 

    public ISDataStoreFactory getDataStoreFactory(); 

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

どちらのアプリは、このインタフェースの独自の実装を持っている、と彼らは低レベルの部分への参照を渡します。下位レベルのパーツが一部のデータストアを開こうとしているときは、ISDataStoreFactoryが返され、返されるのはIMainApp.getDataStoreFactoryです。私はそれが働きたいのと同じように機能します。

誰にでも便利です。

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(); 
} 

これは、いつでもデータをdaに保存することを意味しますtabaseは....任意の時点で保存しています何NewsProfileです....あなたは

おかげので:)

関連する問題