【问题标题】:How to pass data from one activity to another activity by using intents? [duplicate]如何使用意图将数据从一个活动传递到另一个活动? [复制]
【发布时间】:2014-01-06 19:56:38
【问题描述】:

在这里,我编写了一个代码,通过使用意图将数据从一个活动传递到另一个活动。如果我需要在我的代码中进行任何更正,请告诉我。

   OnClickListener buttonListener = new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent nextIntent = new Intent(getApplicationContext(), SecondActivity.class);
        nextIntent.putExtra("firstname", "Siva");
        nextIntent.putExtra("Secondname", "Kumar");
        startActivity(nextIntent);


        Toast.makeText(getApplicationContext(),"SignIn Button Clicked", Toast.LENGTH_SHORT).show();
    }
};

第二个活动:

OnClickListener backListener = new OnClickListener() {

    @Override
    public void onClick(View v) {

        Intent backIntent = new Intent(getApplicationContext(), MainActivity.class);
        Intent receivedIntent = getIntent();
        Bundle bundleData = receivedIntent.getExtras();
        bundleData.getString("firstname");
        bundleData.getString("secondname");
        startActivity(backIntent);
    }
};

【问题讨论】:

  • 在您的第二个活动中更正您的“第二名”。
  • 有具体问题吗?
  • @laalto 我没有在模拟器中得到输出

标签: android android-intent


【解决方案1】:

你应该这样尝试

  1. 将数据传递给 SecondActivity

    Intent nextIntent = new Intent(getApplicationContext(), SecondActivity.class);
    nextIntent.putExtra("firstname", "Siva");
    nextIntent.putExtra("Secondname", "Kumar");
    startActivity(nextIntent);
    
  2. 在 SecondActivity 中获取数据

    Intent receivedIntent = getIntent();
    String firstname=receivedIntent.getStringExtra("firstname");
    String secondname=receivedIntent.getStringExtra("Secondname");
    

编辑

SecondActivity.jaja

public class SecondActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    Intent receivedIntent = getIntent();
    String firstname=receivedIntent.getStringExtra("firstname");
    String secondname=receivedIntent.getStringExtra("Secondname");

    TextView txtFirst=(TextView)findViewById(R.id.text1);
    txtFirst.setText(firstname);
    TextView txtSecond=(TextView)findViewById(R.id.text2);
    txtSecond.setText(secondname);
    Button back=(Button)findViewById(R.id.backbutton);
    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        finish();


        }
    });

}

}

【讨论】:

  • 不工作。请再看一遍。
  • 它正在工作,但我无法在第二个 Activity 中看到传递的值。
  • 使用Log或toast显示Toast.makeText(SecondActivity.this, firstname,Toast.LENTH_LONG).show();
  • 亲爱的 Ketan,我已经通过使用 Toast 做到了这一点,并且在这方面取得了成功。我想在第二个活动中显示为文本。
  • 这意味着您在 SecondActivity 中获得了价值..对吗?
【解决方案2】:

使用一些字符串变量,如下所示,

OnClickListener backListener = new OnClickListener() {

    @Override
    public void onClick(View v) {

        Intent backIntent = new Intent(getApplicationContext(), MainActivity.class);
        Intent receivedIntent = getIntent();
        String firstName = getIntent().getStringExtra("firstname");
        String secondName = getIntent().getStringExtra("Secondname");
        startActivity(backIntent);
    }
};

现在您的两个字符串变量 firstName 和 secondName 包含您需要的信息。

【讨论】:

  • 对不起,它不起作用...请以您自己的编码风格建议我使用意图进行简单的数据传递...谢谢
  • @user3164399,我已经更新了我的答案,请立即查看。在“第二名”中。你的“S”是大写,在接收部分它很小。更改并再次检查。
  • 我按照您的建议进行了更改...它正在工作,但我无法在第二个意图中看到传递的值。
  • @user3164399,对不起,第二名的拼写错误,我已经更新了,请重试。
  • 仍然面临同样的问题...除了代码还有其他原因吗..???
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 2015-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多