我有一个 Activity DrinkWater
其中包含 recyclerView
。我正在尝试保存 arrayList
命名data
保存在内部存储中以供将来引用,但不知何故它不起作用。
我确实在 list 文件中添加了权限,所以这不是问题。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
下面是 DrinkWater.java
的(简化)代码文件。我出于调试目的做了一些 Toast。因此不要错过//评论
public class DrinkWater extends ActionBarActivity {
Toolbar toolbar;
private RecyclerView recyclerView;
private InfoTodayAdapter adapter;
FileOutputStream fos;
FileInputStream fis;
List<InformationToday> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink_water);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview_today);
adapter = new InfoTodayAdapter(this,getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public List<InformationToday> getData(){
try {
fis = getBaseContext().openFileInput("arrayListToday");
ObjectInputStream ois = new ObjectInputStream(fis);
//Below Toast just for debugging.
Toast.makeText(this,"Try block for read data called 1",Toast.LENGTH_SHORT).show();
data = (List<InformationToday>) ois.readObject(); // Cause of the error.
//Below toast for debugging. But it is never executed.
Toast.makeText(this,"Try block for read data called 2",Toast.LENGTH_SHORT).show();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d("TAG A","arrayListToday file not found");
} catch (Exception e){
Log.d("TAG A","Exception generated 1");
}
return data;
}
public void addWater(View view) { // This method adds data to the recyclerView. It is called by on click of the Floating Action Button
InformationToday i = new InformationToday();
if(view.getId() == R.id.fifty_button)
{
i.volume = "50";
Log.d("TAG A","i.volume = 50");
}
else
{
i.volume = "100";
}
data.add(0,i);
adapter.update(data); //This method is described in recyclerView adapter. It updates the recyclerView data.
try {
fos = getBaseContext().openFileOutput("arrayListToday",Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Toast.makeText(this,"Try block for wrtie data called 1",Toast.LENGTH_SHORT).show(); //For debugging.
oos.writeObject(data); // Cause of the error.
Toast.makeText(this,"Try block for write data called 2",Toast.LENGTH_SHORT).show(); //For debugging. It is never executed.
oos.close();
} catch (FileNotFoundException e) {
Toast.makeText(this,"Catch: File not gound exception",Toast.LENGTH_SHORT).show();
Log.d("TAG A", "Catch: File not found");
e.printStackTrace();
}catch (Exception e){
Toast.makeText(this,"Catch: Exception e",Toast.LENGTH_SHORT).show();
Log.d("TAG A","Catch: Exception e");
}
com.getbase.floatingactionbutton.FloatingActionsMenu f = (FloatingActionsMenu) findViewById(R.id.multiple_actions);
f.collapseImmediately();
}
}
没有这样的错误。但 Logcat 中可能出现的异常:“未知错误(代码 14)无法打开数据库。” 2.“此处不支持读取空字符串” 3.“列 context_id 不唯一(代码 19)”
解决错误(代码 14)无法打开数据库的问题没有帮助。
最佳答案
您必须序列化对象,然后将它们写入文件。
保存(没有异常处理代码):
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(data);
os.close();
fos.close();
尝试一下,然后告诉我这是否适合您。
关于java - Android:将 arrayList 保存到文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32557178/