【问题标题】:How to display strings in activity 3 from activity 1, 2?如何从活动 1、2 中显示活动 3 中的字符串?
【发布时间】:2013-11-08 04:08:44
【问题描述】:

我需要从两个不同的活动中获取字符串值,比如活动1和活动2,每个活动最多应该有4个编辑文本字段..所以总共八个字段应该在活动3中有序显示。我已经尝试了在activity3中没有显示的代码。

看代码,

活动1

String namef = fname.getText().toString();
Intent first = new Intent(AssessmentActivity.this, Second.class);
first.putExtra("list1", namef);
startActivity(first);

String namel = lname.getText().toString();
Intent second = new Intent(AssessmentActivity.this, Second.class);
second.putExtra("list2", namel);
startActivity(second);

String phone = mob.getText().toString();
Intent third = new Intent(AssessmentActivity.this, Second.class);
third.putExtra("list3", phone);
startActivity(third);   

String mailid = email.getText().toString();
Intent fourth = new Intent(AssessmentActivity.this, Second.class);
fourth.putExtra("list4", mailid);
startActivity(fourth);

活动2

String cont = addr.getText().toString();
Intent fifth = new Intent(Second.this, Third.class);
fifth.putExtra("list5", cont);
startActivity(fifth);

String db = dob.getText().toString();
Intent sixth = new Intent(Second.this, Third.class);
sixth.putExtra("list6", db);
startActivity(sixth);

String nation = citizen.getText().toString();
Intent Seventh = new Intent(Second.this, Third.class);
Seventh.putExtra("list7", nation);
startActivity(Seventh);

String subject = course.getText().toString();
Intent Eight = new Intent(Second.this, Third.class);
Eight.putExtra("list8", subject);
startActivity(Eight);

*Activity3*

TextView first = (TextView)findViewById(R.id.textView2);
String fieldone = getIntent().getStringExtra("list1" );
first.setText(fieldone);

TextView second = (TextView)findViewById(R.id.textView3);
String fieldtwo = getIntent().getStringExtra("list2" );
second.setText(fieldtwo);

TextView third = (TextView)findViewById(R.id.textView4);
String fieldthree = getIntent().getStringExtra("list3" );
third.setText(fieldthree);

TextView fourth = (TextView)findViewById(R.id.textView5);
String fieldfour = getIntent().getStringExtra("list4" );
fourth.setText(fieldfour);

TextView fifth = (TextView)findViewById(R.id.textView6);
String fieldfive = getIntent().getStringExtra("list5" );
fifth.setText(fieldfive);

TextView sixth = (TextView)findViewById(R.id.textView7);
String fieldsix = getIntent().getStringExtra("list6" );
sixth.setText(fieldsix);

TextView seventh = (TextView)findViewById(R.id.textView8);
String fieldseven = getIntent().getStringExtra("list7" );
seventh.setText(fieldseven);

TextView eight = (TextView)findViewById(R.id.textView3);
String fieldeight = getIntent().getStringExtra("list8");
eight.setText(fieldeight);

【问题讨论】:

  • 您是否在 Activity3 的 onCreate 中接收它们?同样,当您从 Activity1 接收 Activity2 中的数据时,您需要将它们发送到 Activity 3。
  • 是的,在 Activity3 的 onCreate 中接收它们。它将值从 Activity2 传递到 Activity3,但无法从 Activity1 获取值到 Activity3。不知道如何将 Activity2 中的值从 Activity1 保存到 Activity3 中显示。
  • args.putString("list1",getIntent().getExtras().getString("namef")) 你是这样做的吗?确保使用相同的键名发送和接收
  • 感谢 cmets Omid,代码现在按预期工作。非常感谢。

标签: android


【解决方案1】:

我认为您想将它们全部传递到新活动中,而不是每个都开始新活动。

Intent first = new Intent(AssessmentActivity.this, Second.class);

String namef = fname.getText().toString();
first.putExtra("list1", namef);

String namel = lname.getText().toString();
first.putExtra("list2", namel);

String phone = mob.getText().toString();
first.putExtra("list3", phone);

String mailid = email.getText().toString();
first.putExtra("list4", mailid);

startActivity(first);

然后,在您的第二个(第二个活动)的 onCreate 中,您可以提取其中的每一个并存储它们。然后,当您发送到第三个时,您可以将每个重新添加到意图中。

Intent fifth = new Intent(Second.this, Third.class);

fifth.putExtra("list1", namef_stored);
fifth.putExtra("list2", namel_stored);
fifth.putExtra("list3", phone_stored);
fifth.putExtra("list4", mailid_stored);

String cont = addr.getText().toString();
fifth.putExtra("list5", cont);

String db = dob.getText().toString();
fifth.putExtra("list6", db);

String nation = citizen.getText().toString();
fifth.putExtra("list7", nation);

String subject = course.getText().toString();
fifth.putExtra("list8", subject);

startActivity(fifth);

【讨论】:

  • 你解释的方式让我一看就明白了。非常感谢埃里克。你是个好老师!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 2020-02-23
相关资源
最近更新 更多