How to Pass Integer from One Activity to Another

How to pass integer from one Activity to another?

It's simple. On the sender side, use Intent.putExtra:

Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);

On the receiver side, use Intent.getIntExtra:

 Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);

android integer value passing from one activity to other issue

Try like this change the Totalsum in your get String

 Intent intent = new Intent(getApplicationContext(),
TotalCalories.class);
intent.putExtra("TotalSum", sum);
startActivity(intent);

Add this inside Oncreate of new activity

  Bundle extras = getIntent().getExtras();
int sum = Integer.parseInt(extras.getString("TotalSum"));
TextView textview = (TextView) findViewById(R.id.textViewname);
textview.setText("Your OutPut"+sum);

How to pass int value from one activity to another and display in TextView?

Try this,

   String baVal = intent.getExtras().get("yzValue").toString() ;

Android: Passing int value from one activity to another

You should pass your data as an extra attached to your intent. To do this you need to first determine a global key to be used. You could do something like this in your MainActivity

public static final String SOME_KEY = "some_key";

then modify your OpenStats method to

public void OpenStats(View view) {
Intent getStats = new Intent(this, Stats.class);
getStats.putExtra(SOME_KEY, n);
startActivity(getStats);
}

and then in Stats.class onCreate method

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.stats);

int n = getIntent().getIntExtra(MainActivity.SOME_KEY, -1);
TextView tvStats = (TextView) findViewById(R.id.passedNumber_textview);
String number = String.valueOf(n);
tvStats.setText(number);
}

You obviously should make sure that you are calling ButtonRoll at least once or that you set n so that you aren't passing a null int.

Also, as note, convention states that methods should use lower camel case formatting. That is, the first word is completely lower case and the first letter of subsequent words is upper case. That would change your methods

OpenStats() -> openStats()
ButtonRoll() -> buttonRoll()

Classes/objects are upper camel case, just to help avoid confusion.

How to pass an integer from one activity to another in android application

You can't pass a value from first activity to third activity directly.First you have to pass it to first-> second then second-> third.

Else there is a simple solution like you can store it on Shared Preferences
Store sum value on shared preferences while moving from first activity to second activity.Then store the expenses value on shared preferences when you move from second to third activity.After that in onCreate method of third activity you can get the both values and subtract.

Passing an int value from one activity to another

Sender Activity:

Intent intent = new Intent(Sender.this, Reciever.class);
intent.putExtra("int_key", intValue);
startActivity(intent);

Receiver Activity:

 int value = getIntent().getIntExtra("int_key", 0); //0 important to use

How to pass int value from one activity to another activity's Text View?


The reason:

In SecondActivity you put a double extra called "tempthing" to intent extras using putExtra(String key, Double value) method. If you will hold mouse over putExtra method you will see that it uses variant with double argument.

double temp = ((fahrenheit - 32) * 5 / 9);
[...]
myintent.putExtra("tempthing", temp); <- puts double

But in your ThirdActivity you're asking for an int extra using getIntExtra method.

int intValue = myintent.getIntExtra("tempthing", 0);

There is no int extra called "tempthing" inside - there is however a double extra with that key.

Solution:

So either you will put the int value in SecondActivity:

myintent.putExtra("tempthing", (int)temp);

or get a double value in ThirdActivity:

double doubleValue = myintent.getDoubleExtra("tempthing", 0);

They just need to match. If you put double - read double. If you put int - read int :)

How to pass List Integer array from one activity to another activity

Pass to intent as follows:

Intent intent = new Intent(merchandise.this, PurchasedViewCart.class);  
intent.putIntegerArrayListExtra("myList", (ArrayList<Integer>) var_Pposition );

Retrieve data as follows:

ArrayList<Integer> test = getIntent().getIntegerArrayListExtra("myList");


Related Topics



Leave a reply



Submit