我在主要活動中保存了一些值,我想將它加載到次要活動中,但我不知道如何去做。這些值將加載到相同的活動中。任何人都可以幫助我。在另一個活動中加載保存的數據
非常感謝!
主要活動:
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);
}
}
第二活動:
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);
}
你看過包嗎? http://stackoverflow.com/questions/17022274/passing-values-through-bundle-and-get-its-value-on-another-activity – ElliotM
你應該考慮使用SharedPreferences。這是更容易。事實上,這就是它的目的。 – durbnpoisn
我認爲一些小小的遺失,但我不知道什麼 – myworld