Android:无法将 arrayList 保存到文件
Android: Saving arrayList to file not working
我有一个包含 recyclerView
的 activity DrinkWater
。我正在尝试将名为 data
的 arrayList
保存到内部存储中的文件以供将来参考,但不知何故它不起作用。
我确实在 Manifest 文件中添加了权限,所以这不是问题所在。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
下面是 DrinkWater.java
文件的(简化)代码。为了调试目的,我做了几个 Toast。因此不要错过 //comments
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 的可能例外:"unknown error (code 14) could not open database." 2. "Reading a null string not supported here" 3."column context_id is not unique (code 19)"
所以解决错误(代码 14)无法打开数据库的问题没有帮助。
您必须序列化对象,然后将它们写入文件。
正在保存(w/o异常处理代码):
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(data);
os.close();
fos.close();
试试这个,如果它适合你,请告诉我。
Reference Link
我有一个包含 recyclerView
的 activity DrinkWater
。我正在尝试将名为 data
的 arrayList
保存到内部存储中的文件以供将来参考,但不知何故它不起作用。
我确实在 Manifest 文件中添加了权限,所以这不是问题所在。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
下面是 DrinkWater.java
文件的(简化)代码。为了调试目的,我做了几个 Toast。因此不要错过 //comments
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 的可能例外:"unknown error (code 14) could not open database." 2. "Reading a null string not supported here" 3."column context_id is not unique (code 19)"
所以解决错误(代码 14)无法打开数据库的问题没有帮助。
您必须序列化对象,然后将它们写入文件。
正在保存(w/o异常处理代码):
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(data);
os.close();
fos.close();
试试这个,如果它适合你,请告诉我。
Reference Link