如何在Android中将数据从一个活动传输到另一个活动?

时间:2015-05-22 12:49:44

标签: java android alertdialog

我想将我的“结果”数据从我的第一个(主要)活动转移到 我的Custaddress活动,其中包含客户详细信息的编辑文本,然后将其发送到电子邮件。电子邮件/编辑文本完美无缺 - 但我想 将“result.toString”添加到电子邮件正文字符串中。如何将“结果”转移到第二个活动?我相信它与arg有关吗? 这是我第一次活动的代码..

DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);
          result.append("\nTotal: £"+decimalFormat.format(totalamount));
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
      alertDialogBuilder.setMessage(result.toString());
      alertDialogBuilder.setTitle("YOUR ORDER");
      alertDialogBuilder.setPositiveButton("Accept", 
      new DialogInterface.OnClickListener() {

         @Override
         public void onClick(DialogInterface arg0, int arg1) {
            //do what you want to do if user clicks ok
             //Intent intent = new Intent(context, Custaddress.class);
            // startActivity(intent);
             Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
             startActivity(custaddress);
         }
      });
      alertDialogBuilder.setNegativeButton("Decline", 
      new DialogInterface.OnClickListener() {

         @Override
         public void onClick(DialogInterface dialog, int which) {
            //do what you want to do if user clicks cancel.
         }
      });

      AlertDialog alertDialog = alertDialogBuilder.create();
      alertDialog.show();

6 个答案:

答案 0 :(得分:1)

将其写入传递数据的活动

Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
custaddress.putExtra("key",value);
startActivity(custaddress);

在捕获数据的活动中写下以下代码

Intent intent=getIntent();
String mString=intent.getStringExtra("key");

希望这会对你有所帮助

答案 1 :(得分:0)

你应该使用意图(click here)

Intent intent = new Intent(getBaseContext(), CustAdrresActivity.class);
intent.putExtra("text", mytext);
startActivity(intent);

答案 2 :(得分:0)

您只需更换行:

Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
startActivity(custaddress);

这三行:

Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
custaddress.putExtra("result", result.toString());
startActivity(custaddress);

然后,当您打开新活动(在您的情况下为Custaddress活动)时,您应该执行以下操作以获取result

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("result");
}    

答案 3 :(得分:0)

您应该在您在startActivity(intent)方法中传递的Intent对象中添加Extra。 实施例

String value = "String i want to send to next activity"
Intent intent = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
intent.putExtra("KEY", value);
startActivity(intent);

在Custaddress.java Activity类中,您需要从onCreate(Bundle bundle)方法中获取的Bundle对象中获取数据

Bundle extras = getIntent().getExtras();
if (extras != null) {
    // get data via the key
    String valueFromPreviousActivity = extras.getString("KEY");

    if(valueFromPreviousActivity != null){
         // do something with the data
    }
}

答案 4 :(得分:0)

查看此官方文档Starting Another Activity

答案 5 :(得分:0)

通过以下代码,我们可以在活动之间发送值

在父活动中使用以下代码

Intent myintent=new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);

在儿童活动中使用以下代码

String s= getIntent().getStringExtra(<StringName>);
相关问题