2

オブジェクトのリストを Android アプリケーションに保存する最も簡単な、またはネイティブな方法は何ですか?

保存してから、アプリの再起動時にロードする必要があります。

誰かが私のためにすべてを作る準備ができているライブラリを持っているといいでしょう。

4

3 に答える 3

4

あなたのアイテムを道具に保管して、LinkedListあなたParcelableの中に入れることができますbundle. onSaveInstanceオブジェクトを次のように使用できますbundle

bundle.putParcelableArrayList ("key", list);

そして、次を使用してデータを取得しonRestoreInstanceStateます:Activitybundle

bundle.getParcelableArrayList();
于 2012-05-30T15:10:29.553 に答える
3

通常、完全な回答は提供しませんが、クリップボードにあるため...

これは、どの List 実装でも機能するはずです。

さらに、定義/交換する必要がありConstants.EXTERNAL_CACHE_DIR、本番環境では UI スレッド以外のものを使用したい場合があります。

 public static void persistList(final List<String> listToPersist, final String fileName) {
    FileWriter writer = null;
    BufferedWriter bufferedWriter = null;
    File outFile = new File(Constants.EXTERNAL_CACHE_DIR, fileName);
    try {
      if (outFile.createNewFile()) {
        writer = new FileWriter(outFile);
        bufferedWriter = new BufferedWriter(writer);
        for (String item : listToPersist) {
          bufferedWriter.write(item + "\n");
        }
        bufferedWriter.close();
        writer.close();
      }
    } catch (IOException ioe) {
      Log.w(TAG, "Exception while writing to file", ioe);
    }
  }

  public static List<String> readList(final String fileName) {
    List<String> readList = new ArrayList<String>();
    try {
      FileReader reader = new FileReader(new File(Constants.EXTERNAL_CACHE_DIR, fileName));
      BufferedReader bufferedReader = new BufferedReader(reader);
      String current = null;
      while ((current = bufferedReader.readLine()) != null) {
        readList.add(current);
      }
    } catch (FileNotFoundException e) {
      Log.w(TAG, "Didn't find file", e);
    } catch (IOException e) {
      Log.w(TAG, "Error while reading file", e);
    }
    return readList;
  }
于 2012-05-30T15:06:26.453 に答える
0

共有設定を試しましたか..? アプリが終了しようとしているときに、共有設定を使用して保存し、アプリの起動時にその設定を再度読み込みます。

于 2012-05-30T15:07:32.420 に答える