1

I am currently working on a 10x10 grid and trying to make alterations to boxes of it. I have 3 classes operating for this operation but I can't change the view after making the changes. 3 classes:

  1. Main (where I do changes)

    placeButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            mGame.place(size, isHorizontal, row, column);});
    
  2. Game (where I handle and apply the changes)

    System.out.println("Place Ships: " + size + isHorizontal + left + top);
    
    if(isHorizontal == true) {//if horiz
        if(gameGrid[top][left] ==0) {
            for (int i = 0; i < size; i++) {
                gameGrid[top][left + i] = size;
            } }
        else{
            System.out.println("ALREADY FILLED");
        }
    
  3. View (where I draw and paint my grid)

    System.out.println("SETUP VIEW ARRAY");
    for (int col = 0; col < colCount; col++) {
    
        for (int row = 0; row < rowCount; row++) {
    
            int x = mGame.placedCell(row, col);
            System.out.print(x + " ");//tocheck if the array obtained correctly
            float cx = separatorSize + (chosenDiameter + separatorSize) * col;
            float cy = separatorSize + (chosenDiameter + separatorSize) * row;
    
            if(x > 0){
                System.out.println("FOOOOOOOOOOOUND! ");   //received fine
                paint = mPlayer1Paint;
            }else {
                paint = mBGPaint;
            }
            canvas.drawRect(cx, cy, oneSide + (cx), oneSide + (cy), paint);
        }
        System.out.println("");
    }
    System.out.println("SETUP VIEW ARRAY END");   
    
  4. placedCell method in my game class

        mainActivity.setRow(row);
        mainActivity.setCol(column);
    
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                int flag = gameGrid[row][column];
                if(i == column && j == row){
                    if(flag > 0){ //because unedited places is 0, edited places are more than 0
                        gameGrid[row][column] = flag;
                    }
                } } }
        return gameGrid[row][column];
    

My issue is my view class can't obtain the changed array (gameGrid) from my game class. I tried to print the array to check if it has changed correctly and it is. I also declared the array in my Game class as static but nothing has changed. Any kind of help is appreciated.

1 Answer 1

1
  • In order to make the changes in a View class, one way is to pass the reference of the entire view class to your Game class where you are making the change in the array gameGrid.

  • Then you can call view.invalidate() to redraw your grid when you update the array. invalidate() will call the onDraw() method of the view class again.

You can pass the view reference inside the constructor of your game class-

MyGameView mView; 
    public Game(MyGameView view)
    {
    mView=view;
     ...
    }

Or create a method inside Game class to set the value of mView after your view is created-

public void setViewReference(MyGameView view)
{
mView=view;
}

Now, create a public method inside your View class that will receive the updated array and redraw the entire grid again-

public void update(int [][] updatedGameGrid) //Assuming the array is int. Change the datatype is its not an int array
{
   gameGrid=updatedGameGrid; //Here gameGrid is the array inside your view class
   invalidate(); // Redraw the entire View (grid) again by calling onDraw()
}

Now, in your Game class, you can update the array and pass it to the view class

public void update() 
{
 ... //Place you code to update the gameGrid array
 mView.update(gameGrid); //Pass the array to view class
}
Sign up to request clarification or add additional context in comments.

10 Comments

I have followed each step you mentioned but still, nothing has changed. I also had to declare a new int[][] array with elements in my View class because at the first opening it tries to read a null array and application crashes. After doing that, nothing happened. the program works fine like before, the actual grid is updated in the Game class but doesn't change the view class's array elements again. Also the update method in Game class looks like it's unused (underlined and gray), can it be something about it?
The update () method inside game class is very essential. It passes the updates to the View class. What you need to do after making changes to your array is to call the update method of game class. mView.update(gameGrid); // This is necessary to pass the array to view class.
Refer to the 2nd point of your code where you are handling and applying changes to the array. Call the update() method of Game class after changing array
Okay, I did those but now I am having an exception. java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.aiox.shippies.SetupView.update(int[][])' on a null object reference
I did the exact same thing and that's causing an exception
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.