Read inputstream created in another activity

0

I'm new to programming.
I saved an array of strings through FileOutputStream.write() in my MainActivity . I can read the array perfectly through FileInputStream in the Activity itself. To read this array in another Activity I used the following code:

// READ
FileInputStream fin;
fin = openFileInput("my_file");
ObjectInputStream ois = new ObjectInputStream(fin);
String[] temp = (String[]) ois.readObject();
ois.close();
String texto = temp[5].replace("-", " ");
TextView tv = (TextView) findViewById(R.id.a2);
tv.setText(texto);

Eclipse pointed to the following error:

  

The method openFileInput (String) is undefined for the type AdapterPages

Then I made the following modification in the code (maybe there is the error):

MainActivity main = new MainActivity();
Context contexto = main.getApplicationContext();
FileInputStream fin;
fin = contexto.openFileInput("my_file");

It works "without errors", but the text of TextView tv does not change.

UPDATE 1
Save the array :

// WRITE NEW FILE
FileOutputStream fout = openFileOutput("my_file",
        Context.MODE_APPEND);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(mVal);
oos.close();

UPDATE 2
Call the other activity :

// RU call button
public void btTela1(View v) {
startActivity(new Intent(this, Pager.class).putExtra("dados", texto));
}
    
asked by anonymous 04.01.2015 / 21:24

1 answer

0

MY SOLUTION
As I said in the question comments, the main problem was to read FileInputStream in a class that does not extends Activity . Could not get context and then gave error. I even found several solutions to get the context , but maybe because this class was a PagerAdapter , it complicated a little and it did not work.

My final goal was to change data in a TextView of an layout inflated in that class.

From the answer of a question in a situation almost identical to mine, it was possible to solve the problem.

I made some changes to the structure of ViewPager and then I was able to modify the TextView I want in the class that directs PagerAdapter , which performs extends Activity . So now, by having the context , I can both read FileInputStream and pass data by Intent .

The question and answer I have used better clarify what I am trying to explain: link

    
07.01.2015 / 18:32