-1

im having a problem in passing information from one activity to another ... the idea is to choose from a vegetable market what do you need and choose the quantity this pic explain Activity 1

i want to take all the information from the first activity so when i choose banana for example then put the quantity click on add to shopping cart the next activity would be like this Activity 2

so how code i take the information from the first activity to the other ?!

Reema
  • 3
  • 1
  • there are several ways: http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android http://stackoverflow.com/questions/8573796/keeping-a-variable-value-across-all-android-activities. Depends of your needs. – Tobiel Jul 24 '13 at 23:18

2 Answers2

1

When you start a new activity, you can send a bundle with it. Bundles can contain data like strings, booleans, ints, pretty much anything.

For example:

Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("value_1", value1);
bundle.putInt("value_2", value2);
intent.putExtras(bundle);
this.startActivity(intent);

And in your second activities onCreate method, you can get the values from the bundle like:

@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);

Bundle bundle = getIntent().getExtras();
String value1 = bundle.getString("value_1");
String value2 = bundle.getString("value_2");
athor
  • 6,848
  • 2
  • 34
  • 37
  • ok thanks vary vary match but could you give me hint for using just only arrays or using the method put extra for passing information form one activity to another – Reema Jul 24 '13 at 23:22
  • To pass arrays around just use the method of the bundle to put an array,e.g.: bundle.putStringArray(key, value); bundle.putIntArray(key, value);(key, value); – athor Jul 24 '13 at 23:32
  • If it answers your question you can choose the tick above to accept the answer :) – athor Jul 25 '13 at 00:06
0

First associate EditText to a variable:

`EditText bananas = (EditText)findViewById(R.id.bananastext);`

Then get value of it:

`int value = Integer.parseInt(bananas.getText().toString());`

Then create an Intent and attach on it value:

`intent.put("bananas",value);`

Then from the other activity create a Bundle and get value:

Bundle extras = getIntent().getExtras;
Int value = extras.getInt("bananas");`
Stefano Munarini
  • 2,711
  • 2
  • 22
  • 26