Skip to content

Instantly share code, notes, and snippets.

@gabu
Created August 5, 2012 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabu/3261742 to your computer and use it in GitHub Desktop.
Save gabu/3261742 to your computer and use it in GitHub Desktop.
ちゃんとArrayListの中身までデシリアライズできるから不思議!
public class CopyOfRecipe056Activity extends ListActivity {
ItemAdapter mAdapter;
ArrayList<Item> mItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
mItems = new ArrayList<Item>();
} else {
// ちゃんとArrayListの中身までデシリアライズできるから不思議!
mItems = (ArrayList<Item>) savedInstanceState.getSerializable("items");
}
mAdapter = new ItemAdapter(this, mItems);
setListAdapter(mAdapter);
}
class Item {
int resId; // アイコン画像リソースID
String name; // ユーザ名
String comment; // コメント
Item(int resId, String name, String comment) {
this.resId = resId;
this.name = name;
this.comment = comment;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// リストデータを保存
outState.putSerializable("items", mItems);
}
}
@gabu
Copy link
Author

gabu commented Aug 5, 2012

Itemクラスをimplements Serializableしなくてもフィールドの値までちゃんと復元できる点が不思議です。
ArrayListが頑張ってるのかなぁ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment