简体   繁体   English

什么作为Context参数传递给文件I / O方法?

[英]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. 感觉真的很愚蠢,不知怎的,我有一种感觉,我应该把“这个”作为“上下文”的论据,但说实话,这整个概念对我来说是新的,因为它让我感到困惑,我宁愿知道100%什么和为什么在我开始无休止地寻找语法错误之前。

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. 几天前我问过一个关于如何使用文件输入/输出流将类对象的ArrayList的内容写入应用程序启动时要检索的文件的问题。 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. 我对我的代码感到非常自信,直到我意识到在调用我的保存和检索方法时我必须传递一个Context。 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. 我假设我可以调用第一个方法将ArrayList保存到文件,然后调用第二个方法简单地将保存的值加载到数组中。 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"? 因此,根据我对原始问题的回答,这两个方法都需要在上下文中传递,而Java对于获取该上下文非常坚定(如图),所以当我调用updatePickThreeList(Context)时,究竟是什么进入“背景”?

Thanks in advance - I'm one very appreciative programming noob. 提前谢谢 - 我是一个非常感激的编程菜鸟。

so when I call updatePickThreeList(Context);, what exactly goes in as the "Context"? 所以,当我调用updatePickThreeList(Context);时,究竟是什么作为“上下文”?

An instance of some class that extends Context . 扩展Context的某个类的实例。 Depending on where you are calling this code, that might be an Activity or a Service . 根据您调用此代码的位置,可能是ActivityService

BTW, please be careful when using static data members, so you do not introduce memory leaks. 顺便说一下,使用静态数据成员时请小心,这样就不会引入内存泄漏。

Also, please do disk I/O on a background thread (eg, AsyncTask or IntentService ). 另外,请在后台线程(例如, AsyncTaskIntentService )上执行磁盘I / O.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM