[英]Android: Saving arrayList to file not working
I have an activity DrinkWater
that contains recyclerView
. 我有一个活动
DrinkWater
包含recyclerView
。 I am trying to save the arrayList
named data
to file in Internal storage for future reference but somehow it isn't working. 我正在尝试将名为
arrayList
data
保存到内部存储中的文件中,以备将来参考,但不知如何。
I did add the permissions in the Manifest file so thats not the problem. 我确实在清单文件中添加了权限,所以那不是问题。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Below is the (Simplified) code for the DrinkWater.java
file. 以下是
DrinkWater.java
文件的(简体)代码。 I made few Toasts for debugging purpose. 我为调试目的制作了一些Toast。 Therefore do not miss the //comments
因此不要错过// 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();
}
}
No errors as such. 这样就没有错误。 BUT Probable exceptions from Logcat: "unknown error (code 14) could not open database."
但是Logcat可能出现的异常:“未知错误(代码14)无法打开数据库。” 2. "Reading a null string not supported here" 3."column context_id is not unique (code 19)"
2.“读取此处不支持的空字符串” 3.“列context_id不是唯一的(代码19)”
SO question for resolving error (code 14) could not open database didnt help. 因此,解决错误(代码14)无法打开数据库的问题没有帮助。
You would have to serialize the objects and then write them into the file. 您将必须序列化对象,然后将它们写入文件。
Saving (w/o exception handling code): 保存(不包含异常处理代码):
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(data);
os.close();
fos.close();
Try this out and let me know if this works for you. 试试看,让我知道这是否适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.