嗨在我的应用程序中,我保存了用户输入的字符串信息。然后序列化这些字符串以供稍后检索。当我打开文件到他们保存的位置时,我总是只收到输入的最后一个字符串。你能看到我要去哪里吗?无法检索所有保存的对象,并显示在列表中
这是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