Accessing an ArrayList after clicking in android application

Go To StackoverFlow.com

0

I'm currently trying to create a 3d game in android and I'm struggling with some issues.

At some point after clicking on a direction button in my game, I try to access an ArrayList but it seems that my ArrayList(that was filled earlier) is now empty.

Is that a threading issue?

@Override 
public void Update(ArrayList<ArrayList<IModel>> Database) {
    this.DataBase = Database;
    System.out.println("---> " + this.DataBase.get(0));  //WORKING                             
}


Button up_btn = (Button) findViewById(R.joystick.up);
up_btn.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        System.out.println("--------->" + DataBase.get(0)); //NOT WORKING   
    }
});

here's my whole code ;

 public class PlayerController extends Activity implements IController
 {
//Properties

private ArrayList<ArrayList<IModel>> DataBase = null;
private GLSurfaceView mGLView;

//Virtual

@Override public void Update(ArrayList<ArrayList<IModel>> Database)
{
    this.DataBase = Database;
    System.out.println("---> " + this.DataBase.get(0));                             //WORKING
}

@Override protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.mGLView = new MySurfaceView(this);

    LayoutInflater inflater = getLayoutInflater();
    this.getWindow().addContentView(this.mGLView, new ViewGroup
            .LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT));
    this.getWindow().addContentView(inflater.inflate(R.layout.footer, null), new ViewGroup
            .LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT));
    this.getWindow().addContentView(inflater.inflate(R.layout.play, null), new ViewGroup
            .LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, (int)80));

    //Joystick buttons

    Button up_btn = (Button) findViewById(R.joystick.up);
    up_btn.setOnClickListener(new View.OnClickListener()
    {
        @Override public void onClick(View v)
        {
            System.out.println(">>>>>" + DataBase.get(0));           //NOT WORKING..
            //UpMotionListener();
        }
    });

}

@Override protected void onPause()
{
    super.onPause();
    mGLView.onPause();
}

@Override protected void onResume()
{
    super.onResume();
    mGLView.onResume();
}

public void UpMotionListener()
{
    //System.out.println(">>>>>>>>>" + DataBase.get(0));                //NOT WORKING
}
2012-04-05 19:13
by user1316055


1

this in the second case is the instance of the OnClickListener, so you need to fully qualify it, or remove this at all, e.g.:

EnclosingClass.this.dataBase.get(0);

or

dataBase.get(0);

Please note that I started the variable name with lower case, to match Java's naming conventions.

2012-04-05 19:15
by MByD
hi binyamin ,apparently,it doesn't come from that, it really looks like my list has been erased from the memory,maybe i need a dispatcher for the onclick even - user1316055 2012-04-05 19:24
I found the solution : put the key word 'static' in front of the ArrayList DataBase.

Thank you anyway for your answe - user1316055 2012-04-05 21:11



0

Well, really your problem is that you don't understand the Java language, however that is fixable. Here is a quick overview of the specific knowledge you are missing: http://www.oursland.net/tutorials/java/innerclasses/

Now, to answer your question directly:

Try referring to Database without the "this." in front of it.

this.DataBase.get(0) in your new View.OnClickListener() is referring to your anonymous inner class created when you said new OnClickListener, not the instance class where you created you Database member. When you use "this" you are saying this class and since this is inside your listener it is referring to the class created inline and the "Database" member variable is out of scope.

2012-04-05 19:25
by Brill Pappin
oh sorry i made a mistake in my code,what i meaned was that System.out.println("--------->" + DataBase.get(0)); is not workin - user1316055 2012-04-05 19:42
I found the solution : put the key word 'static' in front of the ArrayList DataBase. Thank you anyway for your answe - user1316055 2012-04-05 21:11


0

Could it be that something else is modifying/clearing the ArrayList in-between the time Update and onClick are called?

2012-04-05 19:36
by gngr44
Thanks for your answer. No my class just get the arraylist with the Update method but when I click on the button the OnClick method seems to read an empty arraylist - user1316055 2012-04-05 19:57
I found the solution : put the key word 'static' in front of the ArrayList DataBase. Thank you anyway for your answe - user1316055 2012-04-05 21:11
Ads