简体   繁体   中英

Problem accessing variable[] from another class

I know this a pretty basic question, and already found another ones like mine, but I honestly don't know what I'm doing wrong.

public class InteractiveArrayAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;

    public int teste;

    public InteractiveArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.rowbuttonlayout, list);
        this.context = context;
        this.list = list;

    }

    public int getTest()
    {
        return teste;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        teste = 2;
....
}
}

and other class:

try{

                InteractiveArrayAdapter adapt = new InteractiveArrayAdapter(this,
            getAPPS(0));
                int test = adapt.getTest();
                Toast.makeText(this, Integer.toString(test), Toast.LENGTH_LONG).show();
                Log.v("TAG",Integer.toString(test));
            }catch(Exception e)
                {
                Log.v("EXCEPTION",e.toString());
            }

EDIT: I was getting null for a stupid mistake, and now I'm getting the primitive and expected 0 as most of you say.

At some point of my app, everytime a checkboxes is clicked that method getView is executed. I want to store that to an array[] of strings progressively (i+1) (i just put int to be easier to understand - realize now it was a mistake), and then when users inputs ok I want to access the whole array. Wondering if it's possible the way I want.

So when I do this

InteractiveArrayAdapter adapt = new InteractiveArrayAdapter(this,
                getAPPS(0));

This is meaningless, because I don't need to execute anything again, I just want to retrieve the created array - if possible!

Your code won't even compile. return this.teste; should be return this.test; .

Well, this isn't a direct copy/paste, since this obviously wouldn't compile. Whenever you're dealing with an actual error or issue, it's really best to paste the actual code. We're all programmers, so we can read it.

But based on the structure you've shown above, either the typo you've put in the line return this.teste (should be return this.test ) is in your code, or you didn't initialize the instance variable test in your constructor.

Without showing us the actual code you're writing, it's impossible to say (especially the section that initializes the test variable, and the part that returns its value are missing - we're not mind readers, I'm afraid).

So, those are two potential candidates. On another note, however, if you mark the test variable as public, then you don't need to have getter/setter methods for them, since any class can access them without going through a method call. That's what public does.

But that is what should happen according to your code. You don't call B method to update teste variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM