0

I have a class for a board game which sets up a 9x9 grid with an OnClickListener as shown below:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

public class GameActivity extends Activity {

    private ImageAdapter boardAdapter;
    private int[][] board = new int[9][9];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        for(int i = 0; i < 9; i++) {
            board[0][i] = 2;
            board[1][i] = 2;
            board[7][i] = 1;
            board[8][i] = 1;
        }

        GridView gameGridView = (GridView) findViewById(R.id.game_gridview);

        boardAdapter = new ImageAdapter(this, board);
        gameGridView.setAdapter(boardAdapter);

        AdapterView.OnItemClickListener boardListener = new AdapterView.OnItemClickListener() {
            int[][] startPosition;
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(startPosition == null) {
                    startPosition = position; // Returns the value at the position, not the position itself, which is what I am looking for
                }
                boardAdapter.notifyDataSetChanged();
            }
        };

        gameGridView.setOnItemClickListener(boardListener);

    }

}

Here is the code for the custom BaseAdapter I am using to display my 2d integer array in the GridView:

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

    private Context context;
    int[][] board;
    int rowPosition, columnPosition, count;

    public ImageAdapter(Context c, int[][] content) {
        context = c;
        count = 0;
        board = new int[content.length][content[0].length];
        for (int i = 0; i < board.length; i++)
        {
            for (int j = 0; j < board[i].length; j++)
            {
                board[i][j] = content[i][j];
                count++;
            }
        }
        rowPosition = 0;
        columnPosition = 0;
    }

    public int getCount() {
        return count;
    }

    public int getRowCount() {
        return board.length;
    }

    public int getColumnCount() {
        return board[0].length;
    }

    public Object getItem(int rowNum, int columnNum) {
        return board[rowNum][columnNum];
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(120, 120));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        }
        else {
            imageView = (ImageView) convertView;
        }

        columnPosition = position % board[0].length;
        rowPosition = (position - columnPosition) / board[0].length;

        switch(board[rowPosition][columnPosition]) {
            case 1:
                imageView.setImageResource(R.drawable.blackpiece);
                break;
            case 2:
                imageView.setImageResource(R.drawable.whitepiece);
                break;
            default:
                imageView.setImageResource(R.drawable.emptypiece);
                break;
        }

        return imageView;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

}

My issue is that I'm not sure how to get the coordinates in the 2d array when someone clicks on the GridView, for example when the user clicks on the first cell of the gridView it returns the value position[0][0].

1 Answer 1

3

By your question what i understand that u want to convert the position in the respective 2D Array position.I update your OnItemClickListener method.By X and Y position u get respective value from your 2D array. I am assuming that the the grid is 9*9,hence no of Column =9.

Declare class level variable

int columncount=9;

    AdapterView.OnItemClickListener boardListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                int xpos= position/columncount;
                int ypos=position%columncount;
            Log.v("position", "position x is :"+xpos+"  pos y is :"+ypos);

            boardAdapter.notifyDataSetChanged();
        }
    };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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