在我的应用程序中,我尝试使用 onSaveInstanceState 中的 putSerializable 方法保存 ArrayList 状态。并在 onCreate 和 onRestoreInstanceState 中恢复。在我的应用程序中,如果没有保存此对象的实例,它将从互联网数据创建。但是我一直在尝试测试这个解决方案 ArrayList 开始从网络下载。谁能帮我?
public class Activity extends SherlockActivity {
ListView listView;
SlidingMenu slidingMenu;
ArrayList<HashMap<String, String>> newsList = null;
String url = "/////here_my_url////";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setTitle(R.string.news_vatican);
listView = (ListView) findViewById(R.id.newslist);
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setFadeDegree(0.35f);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
slidingMenu.setMenu(R.layout.slidingmenu);
slidingMenu.setBehindOffset(150);
if (savedInstanceState != null){
newsList = (ArrayList<HashMap<String, String>>) savedInstanceState.getSerializable("list");
Log.e("Activity", "RESTORING");
} else {
Log.e("Activity", "PARSING NEW");
JSONParser parser = new JSONParser(this);
parser.execute(url);
try {
newsList = parser.get();
} catch (InterruptedException e) {
e.printStackTrace();
Log.e("Activity", "Failed to get newslist");
} catch (ExecutionException e) {
e.printStackTrace();
Log.e("Activity", "Failed to get newslist");
}
}
listView.setAdapter(new NewsAdapter(getApplicationContext(), newsList));
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerializable("list", newsList);
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
newsList = (ArrayList<HashMap<String, String>>) savedInstanceState.getSerializable("list");
}