我想将字符串从一个活动传递到另一个活动。在第二个活动中,字符串应该是可读的。
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
First actvity code
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Intent dash_des = new Intent(getApplicationContext(),Dashboard_Description__page.class);
//here should be t my string
}
});
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard__description__page);
//here i want to read the string data
}
答案 0 :(得分:2)
你应该阅读Android中的Intents。
您可以使用以下方法实现此目的:
Intent myIntent = new Intent(FirstActivity.this,SecondActivity.class);
myIntent.putExtra("TestString","TestValue");
startActivity(myIntent);
上面的代码将启动SecondActivity。现在在第二个活动的onCreate()中你需要:
String stringCameFromFirstAcvitity = getIntent().getStringExtra("TestString");
stringCameFromFirstAcvitity的值为“TestValue”
在你的情况下
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//First actvity code
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Intent dash_des = new Intent(getApplicationContext(),Dashboard_Description__page.class);
Object obj = listview.getAdapter().getItem(position);
String value = obj.toString();
dash_des .putExtra("TestString",value);
startActivity(dash_des );
}
});
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard__description__page);
//here i want to read the string data
}
secondActivity的代码保持不变。希望它有所帮助
答案 1 :(得分:0)
Intent dash_des = new Intent(getApplicationContext(),Dashboard_Description__page.class);
dash_des.putString("your_key", your_string_variable);
startActivity(dash_des);
在第二项活动中:
Bundle bundle = getIntent().getExtras();
String text = bundle.getString("your_key");
答案 2 :(得分:0)
当有多个字符串需要从一个活动传递到另一个活动时,最好将它们捆绑并传递给下一个活动。 像这样:
Intent in = new Intent(Firstactivity.this, Secondactivity.class);
Bundle mbundle = new Bundle();
mbundle.putString("name", name);
mbundle.putString("phonenumber", phonenumber);
in.putExtras(mbundle);
并且它们可以从第二个活动中检索到如下,
Bundle bundle = getIntent().getExtras();
String name= bundle.getString("name");
String phoneNumber= bundle.getString("phonenumber");
答案 3 :(得分:0)
在意图中将extring作为额外的..
然后将其保留在activity2
中Intent intent = new Intent(current.this, Activity2.class);
intent.putExtra("key","value");
startActivity(intent);
活动2中的:
String data = getIntent().getExtras().getString("key");