Beginner - using variables defined in one class in another class
Solution 1
The simple answer is you have to qualify it with the containing instance's name.
So if the instance is myinstance
to access the currentPrices
field, you would do this:
myinstance.currentPrices
However, you really should be using getters and setters and there are a lot more issues here regarding actually getting a handle on the instance of the activity. I would suggest looking into an intro Java book before you get your hands dirty with Android. Check out http://math.hws.edu/javanotes/ for a good online introduction.
Based on your tag, I assume you have some notion of global variables. There is no such thing in Java. In practice, you can have static
variables that are globally accessible. For instance
public class MyClass{
public static int MY_VALUE = 4;
}
You can access by writing
MyClass.MY_VALUE
However, with instance variables, that is, one's that aren't qualified by the static
keyword, you have have an instance of the class that has been allocated via the new
keyword.
For example
MyClass someInstance = new MyClass();
someInstance.currentPrices
The reason this is more complicated with Activities is because you don't have access to the instance of the activity class that is being used unless you do something really creative.
Does that make sense?
Solution 2
If you ever need to do this without making a static variable, you can also pass the array through the Intent as an extra, like so:
intent.putExtra("currentPrices", currentPrices);
startActivity(intent);
Then, once you're in the new Activity, retrieve it like so:
int[] currentPrices = getIntent().getIntArrayExtra("currentPrices");
It looks like you're modifying the original from the dialog though, so short of using startActivityForResult()
and handling it that way, Chris' method is much simpler.
Eric
Updated on April 18, 2020Comments
-
Eric about 4 years
So, lets say in my main activity, i have an array declared like this, that im not having any problems using inside any of main's methods:
public int currentPrices[] = {0,0,0,0,0,0,0,0,0};
Now, my buyDialog class is as follows:
package foosh.Frontier; import android.app.Activity; import android.os.Bundle; import foosh.Frontier.*; public class buyDialog extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // Be sure to call the super class. super.onCreate(savedInstanceState); currentPrices[0]=999; //insert setup magic here setContentView(R.layout.buydialog); } }
Eclipse persistently says that currentPrices can't be resolved. How do I link a child activity to the parent activity's variables?
I may have done my intent wrong, as I'm quite new at constructing them. But here's that line, from main:
case R.id.buyButton1: currentRes = masterRes.get(0); Intent intent = new Intent(); intent.setClass(main.this, buyDialog.class); startActivity(intent);
-
Eric about 13 yearsThanks for the reply. Yes, I have a java book in the mail already, I realized I was getting in pretty deep pretty quickly :) ---- edit to your edit ---- Makes alot of sense. I had originally tried it "proper" with getters and setters but I still couldnt get the child to resolve back to the parent's methods. I'll tinker around with the New. Thanks.
-
Chris Thompson about 13 years@Eric, happy to help. It definitely can get confusing and Android can be a language all its own at times and can just confuse things further. Good luck!
-
Eric about 13 yearsThe squiggly red line of doom is gone! Found a way to work the instance in. Thank you :)