嗨在我的應用程序中,我保存了用戶輸入的字符串信息。然後序列化這些字符串以供稍後檢索。當我打開文件到他們保存的位置時,我總是隻收到輸入的最後一個字符串。你能看到我要去哪裏嗎?無法檢索所有保存的對象,並顯示在列表中
這是retrival代碼
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lightlist);
adapter = new ArrayAdapter<String>(LightList.this, android.R.layout.simple_list_item_1, lightNames);
refreshBut = (Button)findViewById(R.id.button1);
try {
File file = getCacheDir();
fis = new FileInputStream(new File(file, LightSetup.FILENAME));
ois = new ObjectInputStream(fis);
String a;
while((a = (String)ois.readObject()) != null){
adapter.add(a);
setListAdapter(adapter);
}
ois.close();
} catch (FileNotFoundException e) {e.printStackTrace();} catch (StreamCorruptedException e){e.printStackTrace();
} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}
}//end onCreate
這是序列化代碼
公共無效的onClick(查看爲arg0){
String stringData = data.getText().toString();
try {
File file = getCacheDir();
fos = new FileOutputStream(new File(file,FILENAME));
os = new ObjectOutputStream(fos);
os.writeObject(stringData);
fos.close();
os.close();
} catch (IOException e) {e.printStackTrace();}
Intent i = new Intent("com.Sonny.HCIProject.CreateConfirm");
startActivity(i);
finish();
}//end onClick
發佈您的適配器 – Blackbelt 2012-03-24 16:23:41