オブジェクトのリストを Android アプリケーションに保存する最も簡単な、またはネイティブな方法は何ですか?
保存してから、アプリの再起動時にロードする必要があります。
誰かが私のためにすべてを作る準備ができているライブラリを持っているといいでしょう。
オブジェクトのリストを Android アプリケーションに保存する最も簡単な、またはネイティブな方法は何ですか?
保存してから、アプリの再起動時にロードする必要があります。
誰かが私のためにすべてを作る準備ができているライブラリを持っているといいでしょう。
あなたのアイテムを道具に保管して、LinkedList
あなたParcelable
の中に入れることができますbundle
. onSaveInstance
オブジェクトを次のように使用できますbundle
。
bundle.putParcelableArrayList ("key", list);
そして、次を使用してデータを取得しonRestoreInstanceState
ます:Activity
bundle
bundle.getParcelableArrayList();
通常、完全な回答は提供しませんが、クリップボードにあるため...
これは、どの 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;
}
共有設定を試しましたか..? アプリが終了しようとしているときに、共有設定を使用して保存し、アプリの起動時にその設定を再度読み込みます。