Android: Set Text View's text from another activity Not working

回眸只為那壹抹淺笑 提交于 2020-01-05 04:41:11

问题


I have 2 activities and 2 classes.

In my Main class, when i click submit button it will start another activity. here is the code.

public void onClick(View v) {
    startActivity(new Intent(MainActivity.this, NewActivity.class));
    newActivity.setViewValues(fNameET.getText().toString(), lNameET.getText().toString(), mInitialET.getText().toString(), "Female", "birthday", addressET.getText().toString(), cNumberET.getText().toString());   
}

The newActivity is an object of the other activity and the setViewValues is the method of it.

This doesn't work, this is how i do it in java gui. Maybe something is missing.

Could anyone help me with this?


回答1:


You should pass the data like this.

MainActivity.java

Intent intent = new Intent(MainActivity.this, NewActivity.class));
intent.putExtra("firstName",fvalue);
intent.putExtra("lastname",lname);
......
startActivity(intent);

NewActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layoutname);
    Intent intent = getIntent();
    String firstName = intent.getStringExtra("firstName");
    String lastName = intent.getStringExtra("lastname");
}


来源:https://stackoverflow.com/questions/14672372/android-set-text-views-text-from-another-activity-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!