Search code examples
androidandroid-gridviewlayout-inflater

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?


Solution

  • To make it work I had to,

    A: Create a Context, a LayoutInflater and a constructor inside my "GameplayController" class.

    Context ctxt;
    LayoutInflater myInflater;
    
    public GameplayController(Context c){
        ctxt = c;
        myInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    

    B: Change my "play" method to return View and accept a View as an argument.

    private static boolean gameOver = false;
    
    /*
     * Will run the game loop.
     */
    public View play(GridView gv) { 
        while (gameOver == false) {
            gv.setOnItemClickListener(new OnItemClickListener(){
                public void onItemClick(AdapterView<?> parent, View v, int position, long id){
                    Toast.makeText(ctxt, "" + position, Toast.LENGTH_SHORT).show();
                    ImageView img = (ImageView)v;
                    img.setImageResource(R.drawable.rock1select);
                }
            });
            return(gv);
        }
        return gv;
    }
    

    }

    C: Create a "GameplayController" object in my "MainActivity", passing its Context as an argument. Then call the "play" method, passing it the View created in Main.

    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));
    
        GameplayController gc = new GameplayController(this);
    
        //Create board model, add units to player's list.
        UnitArray gameBoardModel = new UnitArray();
    
        //Run the game loop until winner found.
        gc.play(gridview);
    
    }
    

    For reference for newbies like me here are the import statements used in my main:

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.GridView;
    

    and the ones in my "GameplayController", which is sort of acting like an Image/GridView contoller/adapter:

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.Toast;