Android - problema serializando arraylist de objetos

 
Vista:

problema serializando arraylist de objetos

Publicado por manuel (1 intervención) el 28/11/2013 23:10:01
Estoy realizando un proyecto para el instituto y no soy capaz de hacer funcionar la serializacion de un arraylist de objetos.
Tengo una clase Curriculum, de la cual se crea un objeto y se introduce en un arrayList. Luego se serializa el arrayList.
En otra actividad, des-serializo el arrayList y lo introduzco en una ListView.

En esta ultima actividad, si creo un arraList y creo los objetos directamente en la actividad mediante su constructor y los meto en el arrayList, se me muestran en la lista. Sin embargo, si trato de des-serializar el arrayList y cargarlo en el ListView no muestra nada. El problema pues debe ser la serializacion, que no esta funcionando.

Les dejo a continuación la clase Curriculum, la clase GuardarCargarObjetos (que serializa el arraylist), y la Activity ListadoCV, donde des-serializo el arrayList y se pretende mostrar en una ListView. Tambien les dejo la Activity VistaPrevia, que es donde serializo el arrayList.
Los layouts no los meto porque como les digo, si creo los objetos manualmente en la Activity ListadoCV, los agrego al arrayList y cargo este en el ListView, la lista se muestra perfectamente... luego el fallo ha de estar en la serializacion... :(

En los últimos 3 días he invertido más de 20 horas intentando solucionarlo y he sido incapaz... la verdad es que estoy muy frustrado y no veo modo de arreglarlo, y sobre todo de aprender. Nuestra profesora nunca ha dado Android y de Java anda justita, por lo que no es capaz de resolver ninguna duda... La enseñanza pública española... lamentable. Gracias a todos!

CLASE Curriculum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.dam204.trabajoevaluacion;
 
import java.io.Serializable;
 
public class Curriculum implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1520731881414914436L;
	/**
	 * 
	 */
	// DATOS PERSONALES
	String nombre;
	String apellido1;
	String apellido2;
	String dni;
	String email;
	String direccion;
	int cp;
	String localidad;
	String provincia;
 
	// DATOS ACADÉMICOS
	String titulacion;
 
	// EXPERIENCIA LABORAL
	String experiencia;
 
	// IMAGEN
	int logotipo;
 
	public Curriculum(String nombre, String apellido1, String apellido2,
			String dni, String email, String direccion, int cp,
			String localidad, String provincia, String titulacion,
			String experiencia, int logotipo) {
		this.nombre = nombre;
		this.apellido1 = apellido1;
		this.apellido2 = apellido2;
		this.dni = dni;
		this.email = email;
		this.direccion = direccion;
		this.cp = cp;
		this.localidad = localidad;
		this.provincia = provincia;
		this.titulacion = titulacion;
		this.experiencia = experiencia;
		this.logotipo = logotipo;
	}
 
	public String getNombre() {
		return nombre;
	}
 
	public void setNombre(String nombre) {
		this.nombre = nombre;
	}
 
	public String getApellido1() {
		return apellido1;
	}
 
	public void setApellido1(String apellido1) {
		this.apellido1 = apellido1;
	}
 
	public String getApellido2() {
		return apellido2;
	}
 
	public void setApellido2(String apellido2) {
		this.apellido2 = apellido2;
	}
 
	public String getDni() {
		return dni;
	}
 
	public void setDni(String dni) {
		this.dni = dni;
	}
 
	public String getEmail() {
		return email;
	}
 
	public void setEmail(String email) {
		this.email = email;
	}
 
	public String getDireccion() {
		return direccion;
	}
 
	public void setDireccion(String direccion) {
		this.direccion = direccion;
	}
 
	public int getCp() {
		return cp;
	}
 
	public void setCp(int cp) {
		this.cp = cp;
	}
 
	public String getLocalidad() {
		return localidad;
	}
 
	public void setLocalidad(String localidad) {
		this.localidad = localidad;
	}
 
	public String getProvincia() {
		return provincia;
	}
 
	public void setProvincia(String provincia) {
		this.provincia = provincia;
	}
 
	public String getTitulacion() {
		return titulacion;
	}
 
	public void setTitulacion(String titulacion) {
		this.titulacion = titulacion;
	}
 
	public String getExperiencia() {
		return experiencia;
	}
 
	public void setExperiencia(String experiencia) {
		this.experiencia = experiencia;
	}
 
	public int getLogotipo() {
		return logotipo;
	}
 
	public void setLogotipo(int logotipo) {
		this.logotipo = logotipo;
	}
 
}


CLASE GuardarCargarObjetos

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.dam204.trabajoevaluacion;
 
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.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
 
import android.content.Context;
 
public class GuardarCargarObjetos {
 
	static String filename = "CVsGuardados.dat";
 
	public static void Serializa(Context context, ArrayList<Curriculum> a) {
 
		FileOutputStream fos;
		try {
			//fos = context.getApplicationContext().openFileOutput(filename, Context.MODE_PRIVATE);
			fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
 
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(a);
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
	public static ArrayList<Curriculum> Desserializa(Context context) {
		FileInputStream fin;
		ArrayList<Curriculum> afromfile = null;
 
		try {
			fin = context.openFileInput(filename);
			ObjectInputStream ois = new ObjectInputStream(fin);
			afromfile = (ArrayList<Curriculum>) ois.readObject();
			ois.close();
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (OptionalDataException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		return afromfile;
 
	}
 
}


ACTIVIDAD ListadoCV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.dam204.trabajoevaluacion;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
 
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
 
public class ListadoCV extends ListActivity {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listadocv);
 
		// CREAMOS LA LISTA Y COMPROBAMOS SI EXISTE EL ARCHIVO, SI EXISTE LO
		// CARGAMOS
 
		ArrayList<Curriculum> curriculums = new ArrayList<Curriculum>();
 
		String filename = "CVsGuardados.dat";
		File fichero = new File(filename);
		if(fichero.exists()){
			curriculums = GuardarCargarObjetos.Desserializa(getApplicationContext());
		}
 
 
		  /*String sFichero = "CVs_guardados.dat";
		  File fichero = new File(sFichero);
		  if (fichero.exists()) {
			    
			try {
				curriculums = Serializador.cargarCVs();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			  }	*/
 
 
		 /* curriculums.add(new Curriculum("pepe", "fernandez", "martinez",
		  "53169968-v", "manuelcanadas@gmail.com", "sdgsdgsd", 234235,
		  "sdfsdfd", "sdgsdgds", "sdfgsdgsdg", "dfgdfgdfgdf",
		  android.R.drawable.ic_dialog_info));
		  
		  curriculums.add(new Curriculum("mario", "perez", "sanchez",
		  "34546576-x", "madnufelcganhadjas@hotmail.com", "sdgsdgsd", 287656,
		  "sdfsdfd", "sdgsdgds", "sdfgsdgsdg",
		  "dfgdfhhtgdfbhfdhhttegbrtgdfgdf",
		  android.R.drawable.ic_dialog_info));*/
 
 
		Adaptador adaptador = new Adaptador(ListadoCV.this, curriculums);
		setListAdapter(adaptador);
 
	}
 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.listado_cv, menu);
		return true;
	}
 
}


ACTIVIDAD VistaPrevia

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.dam204.trabajoevaluacion;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
public class VistaPrevia extends Activity {
 
	TextView tv_titulo_previa, tv_nombre_completo, tv_dni, tv_email,
			tv_direccion, tv_cp, tv_localidad, tv_provincia, tv_titulacion,
			tv_experiencia;
 
	String nombre, apellido1, apellido2, dni, email, direccion, localidad,
			provincia, titulacion, experiencia;
	int cp;
 
	String nombre_completo;
 
	Button confirmar, reiniciar;
 
	ArrayList<Curriculum> lista;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vistaprevia);
 
		// CREAMOS LA LISTA Y COMPROBAMOS SI EXISTE EL ARCHIVO, SI EXISTE LO
		// CARGAMOS
		/*lista = new ArrayList<Curriculum>();
		String sFichero = "CVs_guardados.dat";
		File fichero = new File(sFichero);
		if (fichero.exists()) {
			try {
				// lista1 = Serializador.cargarCVs();
				lista = Serializador.cargarCVs();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		lista = new ArrayList<Curriculum>();
		String filename = "CVsGuardados.dat";
		File fichero = new File(filename);
		if(fichero.exists()){
			lista = GuardarCargarObjetos.Desserializa(getApplicationContext());
		}
 
		// RECOGEMOS LOS DATOS ENVIADOS DESDE LA ACTIVIDAD ANTERIOR
		Bundle extras = getIntent().getExtras();
		nombre = extras.getString("nombre");
		apellido1 = extras.getString("apellido1");
		apellido2 = extras.getString("apellido2");
		dni = extras.getString("dni");
		email = extras.getString("email");
		direccion = extras.getString("direccion");
		cp = extras.getInt("cp");
		localidad = extras.getString("localidad");
		provincia = extras.getString("provincia");
		titulacion = extras.getString("titulacion");
		experiencia = extras.getString("experiencia");
 
		// ASIGNAMOS LOS DATOS RECIBIDOS DESDE LA ACTIVIDAD ANTERIOR A LOS
		// TextView DE ESTA ACTIVIDAD
		tv_titulo_previa = (TextView) findViewById(R.id.tv_titulo_previa);
		tv_titulo_previa.setText("CV de " + nombre + " " + apellido1);
 
		tv_nombre_completo = (TextView) findViewById(R.id.tv_nombre_completo);
		nombre_completo = apellido1 + " " + apellido2 + ", " + nombre;
		tv_nombre_completo.setText(nombre_completo);
 
		tv_dni = (TextView) findViewById(R.id.tv_dni);
		tv_dni.setText(dni);
 
		tv_email = (TextView) findViewById(R.id.tv_email);
		tv_email.setText(email);
 
		tv_direccion = (TextView) findViewById(R.id.tv_direccion);
		tv_direccion.setText(direccion);
 
		tv_cp = (TextView) findViewById(R.id.tv_cp);
		tv_cp.setText(Integer.toString(cp));
 
		tv_localidad = (TextView) findViewById(R.id.tv_localidad);
		tv_localidad.setText(localidad);
 
		tv_provincia = (TextView) findViewById(R.id.tv_provincia);
		tv_provincia.setText(provincia);
 
		tv_titulacion = (TextView) findViewById(R.id.tv_titulacion);
		tv_titulacion.setText(titulacion);
 
		tv_experiencia = (TextView) findViewById(R.id.tv_experiencia);
		tv_experiencia.setText(experiencia);
 
		// LINKAMOS LOS BOTONES DEL XML CON EL JAVA
		confirmar = (Button) findViewById(R.id.btn_confirmar);
		reiniciar = (Button) findViewById(R.id.btn_reiniciar);
 
		// BOTON CONFIRMAR
		confirmar.setOnClickListener(new OnClickListener() {
 
			@Override
			public void onClick(View v) {
 
				// implementar la creacion del objeto de la clase Curriculum
				// implementar que se añada el objeto a la lista
				// implementar la serializacion de la lista de objetos
 
				Curriculum nuevoCV = new Curriculum(nombre, apellido1,
						apellido2, dni, email, direccion, cp, localidad,
						provincia, titulacion, experiencia, android.R.drawable.ic_dialog_info);
 
				// lista1.agregarCurriculum(nuevoCV);
				lista.add(nuevoCV);
 
				// Serializador.guardarCVs(lista1);
				//Serializador.guardarCVs(lista);
				GuardarCargarObjetos.Serializa(getApplicationContext(), lista);
 
				Intent intent = new Intent(VistaPrevia.this, Principal.class);
				startActivity(intent);
				finish();
 
			}
		});
 
		// BOTON REINICIAR
		reiniciar.setOnClickListener(new OnClickListener() {
 
			@Override
			public void onClick(View v) {
 
				Intent intent = new Intent(VistaPrevia.this, CrearCV.class);
 
				startActivity(intent);
			}
		});
	}
 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.vista_previa, menu);
		return true;
	}
 
	// SI LA ACTIVIDAD ENTRA EN PAUSA (NOS MOVEMOS A OTRA ACTIVIDAD), LA
	// ACTIVIDAD ACTUAL SE CIERRA
	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder