使用OnItemClickListener从GridView获取2D阵列坐标

时间:2015-04-19 21:58:44

标签: android gridview onitemclicklistener

我有一个棋盘游戏类,它使用OnClickListener设置一个9x9网格,如下所示:

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);

    }

}

以下是我用于在GridView中显示我的2d整数数组的自定义BaseAdapter的代码:

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;
    }

}

我的问题是,当有人点击GridView时,我不确定如何获取二维数组中的坐标,例如当用户点击gridView的第一个单元格时,它返回值位置[0 ] [0]。

1 个答案:

答案 0 :(得分:4)

根据你的问题我理解你想要转换相应2D阵列位置的位置。我更新你的OnItemClickListener方法。通过X和Y位置你从你的2D数组得到相应的值。我假设网格是9 * 9,因此没有列= 9。

声明类级变量

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();
        }
    };