-1

I saved some Values in the primary activity and I want to load it in the secondary activity but i dont know how to do it. The values are loaded in the same activity. Can anyone help me please.

Thank you very much!

primary activity:

public void SaveList(View view) {
    //WriteMethode
    String weight = "Gewicht: " +weightText.getText().toString() + "kg" ;
    String height =   " Körpergröße: "+ heightText.getText().toString() + "cm";

    try {

        FileOutputStream fileOutputStream = openFileOutput("values.txt",MODE_APPEND);
        fileOutputStream.write(weight.getBytes());
        fileOutputStream.write(height.getBytes());
        Toast.makeText(getApplicationContext(),"Gespeichert",Toast.LENGTH_LONG).show();
        fileOutputStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}


public void showlist(View view){


    try {
        FileInputStream fileInputStream = openFileInput("values.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader  bufferedReader= new BufferedReader(inputStreamReader);
        StringBuffer stringBuffer= new StringBuffer();
        String lines;
        while((lines=bufferedReader.readLine()) !=null){
            stringBuffer.append("\n"+lines +"\n");
        }
        resultText.setText( stringBuffer.toString());
    }catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Intent intent = new Intent(this, MyList.class);


    startActivity(intent);

}

}

Second activity:

public class MyList extends AppCompatActivity {
TextView resultLabel;
TextView resultText;
// Button listButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_list);
}
myworld
  • 23
  • 2
  • 10

2 Answers2

0

In android it's more preferable to use Shared Preference to save this kind of app data.

this is a good tutorial to help you understand who it work

0

Use intents, which are messages sent between activities. In a intent you can put all sort of data, String, int, etc.

For example in activity2, before going to activity1, you will store a String message this way :

Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);

In activity1, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :

Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");

Then you can set the text in the TextView:

TextView txtView = (TextView) findViewById(R.id.your_resource_textview);    
txtView.setText(message);
Jitin Jassal
  • 137
  • 2
  • 13