2013-04-08 31 views
1

我想在活动之间传输我在EditText字段中编辑的数据。如果我从活动1转移到活动2,我希望能够在活动2中编辑我从活动1收到的数据,将新数据保存到活动2中并转移到活动3中。如何保存并转移数据在同一时间。现在我可以转移共享首选项,但是我无法保存我刚刚在当前活动中编辑的数据。 请帮忙! 谢谢!如何保存并传输超过5个编辑文本从一个活动到另一个活动的值?

这是用来保存重量在活动1(WEEK1)的代码:

SharedPreferences WeightPreferences = getSharedPreferences("WEEK1", MODE_PRIVATE); 
String r1 = w1.getText().toString(); 
String r2 = w2.getText().toString(); 
String r3 = w3.getText().toString(); 
String r4 = w4.getText().toString(); 
String r5 = w5.getText().toString(); 
SharedPreferences.Editor editor = WeightPreferences.edit(); 
editor.putString("wr1", r1); 
editor.putString("wr2", r2); 
editor.putString("wr3", r3); 
editor.putString("wr4", r4); 
editor.putString("wr5", r5); 
editor.commit(); 

这是用于显示来自活动1中的权重活动2(WEEK2)代码等:

SharedPreferences WeightPreferences = getSharedPreferences("WEEK1", MODE_PRIVATE); 
String string1 = weightPreferences.getString("wr1", null); 
String string2 = weightPreferences.getString("wr2", null); 
String string3 = weightPreferences.getString("wr3", null); 
String string4 = weightPreferences.getString("wr4", null); 
String string5 = weightPreferences.getString("wr5", null); 
w1.setText(string1); 
w2.setText(string2); 
w3.setText(string3); 
w4.setText(string4); 
w5.setText(string5); 
+0

http://stackoverflow.com/questions/15859445 /怎么办 - 你 - 传递一个字符串从 - 一个活动到另一个/ 15859488#15859488。 – Raghunandan 2013-04-08 16:37:21

回答

0

把值到一个数组,并发送这样的:

派:

Intent intent =new Intent(ccurrentClass.this, anotherClass.class); 
Bundle b = new Bundle(); 
b.putSerializable("value", arrayOfString); 
intent.putExtras(b); 
startActivity(intent); 

检索:

Intent intent = getIntent(); 
Bundle b = intent.getExtras(); 
String[][] data = (String[][]) b.getSerializable("value"); 
+0

好的,但我希望能够编辑我收到的号码,将它们保存在当前活动中并进一步传输。然后再次编辑数字,保存在当前活动中并转移到下一个等等。这个解决方案会帮助我吗?谢谢 – george 2013-04-09 09:42:48

+0

这些值在'String [] []'中,你可以按照你的意愿以任何方式修改它,然后按照你的意愿再次通过上述方式从活动到活动多次发送它们。顺便说一句,如果这个答案可以帮助你,接受并赞扬它 – 2013-04-09 10:43:59

+0

是的,它帮助我找到解决方案。我怎么能upvote你?谢谢 – george 2013-04-10 12:07:12

0

您可以使用intent.putExtra(“Name”,Object_with_all_your_content)创建一个包含所有数据的数组列表,并将该对象转移到调用事件中。

我理解你的问题吗?

+0

嗨!谢谢你的回答。我现在有一个bug。所以我有MainActivity这是我的家庭活动从哪里可以访问Activity1和Activity2。我用你的方法将数字从Activity1转移到Activity2,一切工作正常。但是,当我打开主页按钮,并转到MainActivity,如果我尝试从这里打开Activity2,崩溃..我不明白为什么.. – george 2013-04-10 12:57:25

+0

你可以分享logcat错误/异常消息。您可以在例外中看到详细信息。 – 2013-04-10 16:55:02

相关问题