Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

parameters - Android, Can I use putExtra to pass multiple values

I want to pass two values to another activity can I do this with putExtra or do I have to do it a more complicated way, which it seems from my reading. E.g.. can something like this work?

public final static String ID_EXTRA="com.fnesse.beachguide._ID";

Intent i = new Intent(this, CoastList.class);
i.putExtra(ID_EXTRA, "1", "111");
startActivity(i);

The above gives an error.

Edit

The first thing I tried was similar to:

i.putExtra(ID_EXTRA1, "1");
i.putExtra(ID_EXTRA2, "111");

but ID_EXTRA2 seems to write over ID_EXTRA1

So,

i.putExtra(ID_EXTRA, new String[] { "1", "111"});

Looks like the go but how do I extract the values from the array in the second activity, I have been using this for a single value.

passedVar = getIntent().getStringExtra(CoastList.ID_EXTRA);

I guess I have to turn ID_EXTRA into an array somehow???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could pass a 'bundle' of extras rather than individual extras if you like, for example:-

Intent intent = new Intent(this, MyActivity.class);
Bundle extras = new Bundle();
extras.putString("EXTRA_USERNAME","my_username");
extras.putString("EXTRA_PASSWORD","my_password");
intent.putExtras(extras);
startActivity(intent);

Then in your Activity that your triggering, you can reference these like so:-

Intent intent = getIntent();
Bundle extras = intent.getExtras();
String username_string = extras.getString("EXTRA_USERNAME");
String password_string = extras.getString("EXTRA_PASSWORD");

Or (if you prefer):-

Bundle extras = getIntent().getExtras();
String username_string = extras.getString("EXTRA_USERNAME");
String password_string = extras.getString("EXTRA_PASSWORD");

Hope this helps! :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...