Wednesday, April 24, 2013

[android help] How to modify a View from the MainActivity in another class?


I would like update a GridView that I have created in my MainActivity from another class, in my case, called GameplayController.


Specifically, I am trying to simply insert the gridView.setOnItemClickListener(....)... that I currently I have in the MainActivity into a method in the GameplayController class.


I am making a little board game app, and I thought to create a method, "play(), with a loop for the game that runs until the game is over. I started up the GridView in the onCreate of the MainActivity and then I call the method I want the game to run in, but I don't know how to get the GridView I created over to my GameplayController class method "play()" so that I can update it properly directly from there.


Here is the relevant part of my MainActivity:



...
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

//Create board model, add units to player's list.
UnitArray gameBoardModel = new UnitArray();

//Run the game loop until winner found.
GameplayController.play();

//This ClickListener I don't want here, I want it in my play() method.
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int postion,
long id) {
// Toast for development help only.
Toast.makeText(MainActivity.this, "" + postion,
Toast.LENGTH_SHORT).show();
}
});

}
...


Here is the relevant part of the class I want to control the GridView in:



public class GameplayController {

private static boolean gameOver = false;

/*
* Will run the game loop.
*/
public static void play() {
while (gameOver == false) {
/*
* Update the GridView from Main somehow.
*/


/*
* Will be put in correct place later.
/*
gameOver = true;
}
}
}


This may be totally wrong, but will I need to use a LayoutInflater?



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...