I made my own custom class, and I can't figure out what's wrong with it. More specifically my question is what differences exist between making a class for android instead of Java. I mean, according to the logCat it doesn't throw any errors or anything. To the contrary, it tells me it that the InputStream
reads correctly. For some reason I can't get it to save. At all. It doesn't throw errors or anything. It just doesn't save the parameters given. Any ideas? I think it's because I'm declaring the parameters for the method saveData
incorrectly.
package com.eai.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.util.Log;
public class DataHandler extends Activity {
FileOutputStream file_out;
FileInputStream file_in;
ObjectOutputStream obj_out;
ObjectInputStream obj_in;
ArrayListretrieved_data, data_out;
private boolean switch_1;
private String tag;
private String default_message;
Context context;
ArrayListdata_given;
public DataHandler(Context context_given){
context = context_given;
}
@SuppressWarnings("unchecked")
public ArrayListretrieveData(){
tag = "DataHandler";
default_message = "Tested";
try {
file_in = context.openFileInput("array_saved");
obj_in = new ObjectInputStream(file_in);
retrieved_data = (ArrayList) obj_in.readObject();
obj_in.close();
file_in.close();
switch_1 = true;
Log.d(tag, "Loaded");
} catch (FileNotFoundException e) {
Log.d(tag, "File Not Found Exception.");
} catch (IOException e) {
Log.d(tag, "IO Exception.");
} catch (ClassNotFoundException e) {
Log.d(tag, "Class Not Found Exception.");
}
if (switch_1 == false);{
retrieved_data = new ArrayList();
retrieved_data.add(default_message);
}
return retrieved_data;
}
public void saveData(ArrayListdata_given){
try {
file_out = context.openFileOutput("array_saved", 0);
obj_out = new ObjectOutputStream(file_out);
obj_out.writeObject(this.data_given);
obj_out.close();
file_out.close();
Log.d(tag, "Loaded");
} catch (FileNotFoundException e) {
Log.d(tag, "File Not Found Exception.");
} catch (IOException e) {
Log.d(tag, "IO Exception.");
}
}
}
.
stackoverflow.comm
No comments:
Post a Comment