Read inputstream created on another Active

Asked

Viewed 99 times

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 the FileInputStream in 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);

The Eclipse pointed out 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 doesn’t 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));
}
  • how are you doing to use the FileOutputStream? You could post this snippet of code?

  • posted... but I believe that the problem is not in it because as I said in Mainactivity I read the data, but in the other

  • Felipe, my suggestion would be to close Fileoutputstream/Fileinputstream as well. Looking at the documentation, these functions give read/write access to the application, so all Activities should fill the file... Came to debug the return reading?

  • The input and output are close according to the code I posted... I checked in the emulator right, I didn’t even know what the debug is for, I just researched it seems that it also points out the variables and values of it, isn’t that it ? I’ll give a studied debug until tomorrow and put the result of it here... But I’ll leave this question open, can be ? thanks

  • @Wakim I don’t think I know how to debug what you asked... debug shows variable values, as String is not variable, I cannot see the value of the text I get. What should I do?

  • 1

    With breakpoint da to see the value of the array, just change the perspective to debug. From what I read there’s no need to close all the streams, only the last one that was created in cascade. Another suggestion would be to delete the file and create from scratch whenever you write an array. Because you are opening the file in mode APPEND, that is, always writing at the end and the first array written may be empty or old value. This will depend on your logic.

  • Another thing that came to mind, is using this file just to pass data from a Activity to another?

  • @Wakim in MainActivity I can see the content of array with the debug, already in the other Activity no. I closed the stream only at the end as you spoke. Before you start writing the array I always delete the file because it is APPEND. And yes, I just want to pass data on Activity to another.

  • I’ll do a test here, but since deletes the file I see no error in the code. But if it is only to pass data from one Activity to another, it is recommended to use as extra Intent than file. Here at the SO pt have some questions about it, it’s quite simple.

  • @Wakim yes, it would be much simpler, but I’m using a Viewpager, then I can’t imagine how I use it Intent in that case

  • How are you starting the other Activity?

  • @Wakim added the code

  • Felipe, you are already passing extras, just add the array as well.

  • @Wakim I think I understand, but the problem is time to receive. I call the class Pager.class, but the layouts are not inflated on her, she just does the setAdapter(adapter). That is, very complicated (for me). I am trying here in various ways, when I can put the answer. Thank you very much for the help.

Show 9 more comments

1 answer

0


MY SOLUTION
As I had said in the comments of the question, the main problem was to read a FileInputStream in a class that does not extends Activity. It was not possible to obtain the context and then it was a mistake. I even found several solutions to get the context, but perhaps because this class is a PagerAdapter, complicated a little and did not work.

My ultimate goal was to alter data into one TextView of a 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 the ViewPager and then I managed to modify the TextView I want in the class that arrow the PagerAdapter, which extends Activity. So now, by owning the context, I can read the FileInputStream how much data go through Intent.

The question and the answer I used clarify better what I’m trying to explain: https://stackoverflow.com/q/21894580/4407308

Browser other questions tagged

You are not signed in. Login or sign up in order to post.