Saturday, 24 August 2013

What to pass into file I/O method as Context argument?

What to pass into file I/O method as Context argument?

Feeling really stupid, and somehow I have a feeling I'm supposed to pass
"this" as the argument for "Context", but honestly this whole concept is
new to me and as confused as it has me I'd rather know 100% what and why
before I start my endless hunt of syntactical errors.
I had asked a question a few days ago about how to use a file input/output
stream to write the contents of an ArrayList of class objects to file to
be retrieved when the app starts. Basically, I wanted to save entered
values. I was feeling pretty confident in my code until I realized I have
to pass a Context when calling my save and retrieval methods. It was then
the realization I had no idea what a context was hit me... So yeah.
These are my methods: Create the file (I guess?):
private static void updatePickThreeList(Context mC){
FileOutputStream stream = null;
try{
stream=mC.openFileOutput(PICK_THREE_NUMBERS,Context.MODE_PRIVATE);
ObjectOutputStream dout = new ObjectOutputStream(stream);
dout.writeObject(pickThreeNumbers);
stream.getFD().sync();
stream.close();
dout.flush();
}catch(IOException e){
e.printStackTrace();
}
}
Retrieve the data (I hope):
private static void getPickThreeList(Context mC){
FileInputStream stream = null;
try{
stream=mC.openFileInput(PICK_THREE_NUMBERS);
ObjectInputStream din = new ObjectInputStream(stream);
pickThreeNumbers = (ArrayList<PickThreeNumbers>) din.readObject();
stream.getFD().sync();
stream.close();
din.close();
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e1){
e1.printStackTrace();
}
}
And I assume I can call the first method to save the ArrayList to file,
then call the second method to simply load the saved values to the array.
By the way, the arrays and other values used are:
static List<PickThreeNumbers> pickThreeNumbers = new ArrayList();
final static String PICK_THREE_NUMBERS="pick_three_numbers";
So, per the response to my original question, both of these methods are
required to be passed in a context, and Java is being fairly adamant about
getting that context (go figure), so when I call
updatePickThreeList(Context);, what exactly goes in as the "Context"?
Thanks in advance - I'm one very appreciative programming noob.

No comments:

Post a Comment