2015-10-21 14 views
-1

我在主要活動中保存了一些值,我想將它加載到次要活動中,但我不知道如何去做。這些值將加載到相同的活動中。任何人都可以幫助我。在另一個活動中加載保存的數據

非常感謝!

主要活動:

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); 
} 
+0

你看過包嗎? http://stackoverflow.com/questions/17022274/passing-values-through-bundle-and-get-its-value-on-another-activity – ElliotM

+0

你應該考慮使用SharedPreferences。這是更容易。事實上,這就是它的目的。 – durbnpoisn

+0

我認爲一些小小的遺失,但我不知道什麼 – myworld

回答

0

在機器人它更優選的是使用共享偏好來保存這種程序的數據。

this是一個很好的教程,以幫助您瞭解是誰的工作

+0

我知道,但我必須在這個表單中做到這一點任何人都知道我必須做什麼 – myworld

0

使用意圖,這是活動之間發送的消息。在一個意圖,你可以把所有種類的數據,字符串,整數等

例如在activity2,纔去activity1,你將存儲一個字符串這樣的留言:

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

activity1,在onCreate(),您可以通過檢索包(其中包含所有通過調用活動發送的消息)得到的字符串消息,並在其上調用getString()

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

然後你可以設置的T在TextView中的分機:

TextView txtView = (TextView) findViewById(R.id.your_resource_textview);  
txtView.setText(message); 
相關問題