我能够将对象存储到Android设备,但我无法检索它们。这些对象中的数据由用户设置(即String name,int day,ect)。存储这些对象(其密钥是它们的名称),然后在不同的活动中存储它们应该被检索。我无法检索它们,因为在新的活动中我不知道它的名字是由用户决定的。
存储对象(object = person)
FileOutputStream fStream = context.openFileOutput(name,Context.MODE_PRIVATE);
ObjectOutputStream oStream = new ObjectOutputStream(fStream);
oStream.writeObject(person);
oStream.close();
fStream.close();
检索对象(在不同的类中完成)
FileInputStream fStream = context.openFileInput( //where the name of the object should be, here's where I'm stuck );
ObjectInputStream oStream = new ObjectInputStream(fStream);
person = oStream.readObject();
由于我正在进行一项新活动,因此我不知道用户预先确定了他们的名字。我不能简单地在两者中命名关键“人”,因为用户正在制作不确定数量的人并且不断添加和删除人。如果有办法只计算存储对象的数量,或者获取所有存储对象名称的列表,我的问题就会得到解决。
答案 0 :(得分:1)
我假设您正在尝试从某个目录中获取列表中的文件。你可以这样做:
// Get local files
File directory = new File(Environment.getExternalStorageDirectory().toString() + localDir);
File[] listOfLocalFilesArray = directory.listFiles();
// get the names of files
String[] fileArray = new String[listOfLocalFilesArray.length];
for (int i = 0; i < listOfLocalFilesArray.length; i++) {
fileArray[i] = listOfLocalFilesArray[i].getName();
}