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);
You can create the object of one class and using this object you call other class variables and methods, but make sure the accessiblity will be as public. We can use interface class A and by implementing A into B, can use the variable of class A into B.
If you make the variable static, you can use int b = A. impart; . Making the variable static allows you to cross it over to another class without having to get a reference to the class.
You can simply change the access-level to the public or protected (if "class B" is a child of "class A"). Or, you can create getter/setter methods in the classes; which you are already doing.
A class variable is an important part of object-oriented programming (OOP) that defines a specific attribute or property for a class and may be referred to as a member variable or static member variable.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With