Pretty much all day today I've been trying to figure this out but I can't seem to get it right. I understand how to use the File-Output/Input-Stream. However since ArrayList is not an "int" I can't just pass my ArrayList through it. I gather that "serialization" converts my ArrayList to bytes, which can then be manipulated through the File-Output/Input-Stream. I CANNOT figure out how it works though. I stole the "Object-Input/Output-Stream" code from someone else, who seemed to be targeted towards the same goal as I was, but it doesn't work for me. I've looked at serialization in the Android Dev Docs, but cannot figure out exactly how it works.
Can someone please;
A) explain HOW serializing works and/or how to perform it, in more simple terms?
&
B) explain why the below code doesn't work (e.g. BEFORE I added all of the Object(in/out)putStream, my code populated the Listview dynamically and in real time, and NOW it doesn't do anything when the button is pushed, NOR does it load anything, NOR does it throw any errors).
also
C) is there an easier way to store this ArrayList so that it's persistent data.
package com.example.thepicker;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
public class Settings extends Activity implements OnClickListener{
EditText getit;
ImageButton giveit;
ListView thelist;
ArrayList<String> items;
ArrayAdapter<String> list;
Intent pass;
FileOutputStream persistout;
FileInputStream persistin;
ObjectOutputStream osistout;
ObjectInputStream osistin;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
items = new ArrayList<String>();
list = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
pass = new Intent(this,Main.class);
getit = (EditText) findViewById(R.id.getit);
giveit = (ImageButton) findViewById(R.id.giveit);
thelist = (ListView) findViewById(R.id.thelist);
try {
persistin = openFileInput("FILENAME");
osistin = new ObjectInputStream(persistin);
items = (ArrayList<String>) osistin.readObject();
osistin.close();
persistin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
thelist.setAdapter(list);
list.notifyDataSetChanged();
giveit.setOnClickListener(this);
}
@Override
public void onPause() {
super.onPause();
pass.putExtra("total", items);
this.startActivity(pass);
try {
persistout = openFileOutput("FILENAME", Context.MODE_PRIVATE);
osistout = new ObjectOutputStream(persistout);
osistout.writeObject(items);
osistout.close();
persistout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
items.add(getit.getText().toString());
list.notifyDataSetChanged();
}
}