

How to pass an integer variable to another class
source link: https://www.codesd.com/item/how-to-pass-an-integer-variable-to-another-class.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to pass an integer variable to another class
I have one class with public int Result; Later in the game I use that variable to store number of points user makes. Now, I have another activity/class called Result and I have a textView in that activity and I need to use the Result variable to sexText to that textView. I made an instance of my game class and called it g in the Result class and than I did something like this:
score = (TextView) findViewById(R.id.tvResult);
score.setText("Game over!\nYour score is:\n" + k.Result);
But I always get 0. I think it's because I get default value of the variable, not the real one. How can I pass the final value added to my variable after game ends?
I also tried this in the game activity, to pass the variable as an intent:
Intent i = new Intent(Quiz.this, Result.class);
i.putExtra("newResultVariable", Result);
startActivity(i);
Also get 0.
To pass the variable as an intent.
To do programming in any language you should follow naming convention of language.
In Quiz.java
Intent intent = new Intent(context,Result.class);
intent.putExtra("newResultVariable", result);
startActivity(intent);
In Result.java to get value.
public void onCreate(Bundle bundle) {
....
int score = 0;
TextView scoreTextView = (TextView) findViewById(R.id.tvResult);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
score = extras.getInt("newResultVariable", 0);
}
scoreTextView.setText("Game over!\nYour score is:\n" + score);
....
}
Recommend
-
11
Pass parameters to the constructor of a class that is a member of another class advertisements I have class called Bar, and in this class Bar...
-
9
In this article, we will learn how to access variable from another class in java. There are two ways to get variables from another class. Create an object of another class in the main class Extend another class in...
-
8
Where is a private instance variable of an abstract class created in the heap? advertisements abstract class A { private int a;...
-
11
Ruby 3.1 adds try_convert method to Integer class for implicit conversions Aug 3, 2021 , by Jijo Bose 1 minute read Implicit...
-
7
Integer interpreted as a variable advertisements I am working on an audio visual art installation, using a Raspberry Pi with Raspbian Jessie. The a...
-
6
How do I pass an array of variable-sized PROPSHEETPAGE structures to PropertySheet? Raymond November 25th, 2021
-
9
-
5
How to Check if a Variable is an Integer in JavaScript Jan 13, 2022 To check if a variable is an integer in JavaScript, use Number.isInteger(). Number.isInteger() returns true...
-
4
How to generate and pass a JavaScript variable from MVC Controller? advertisements I'm trying to fill a JSON object with a...
-
6
Mio Yasutake 2 days ago CAP deploy error: class java.lang.Integer cannot be cast to class java.lang.String 117 Vie...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK