开发者

Beginner - using variables defined in one class in another class

开发者 https://www.devze.com 2023-02-26 01:50 出处:网络
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:开发者_JAVA百科

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:开发者_JAVA百科

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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消