所以我做了这个自定义的二维数组适配器(基本上是从别人那里复制的,所以我不完全知道我在用它做什么),因为我正在制作一个跳棋棋盘游戏,我希望棋盘是一个二维数组图片浏览量。我只是想知道:我如何在我的主要活动中实际访问这个二维数组中的每个单独的项目。我知道在 OnClick 它给了我选择的视图和位置,但我只是想知道是否有任何方法可以做类似 array[row][column].setBackgroundResource(R.drawable.piecered2)在我的主要活动中。
public class GridViewAdapter extends BaseAdapter {
private Context context;
ImageView[][] gridContent;
int rowPosition, columnPosition, count;
public GridViewAdapter(Context c, ImageView[][] content){
context = c;
count = 0;
gridContent = new ImageView[content.length][content[0].length];
for(int i = 0; i<gridContent.length; i++){
for(int j = 0; j<gridContent[i].length; j++){
gridContent[i][j] = content[i][j];
count++;
}
}
rowPosition = 0;
columnPosition = 0;
}
public int getCount() {
return count;
}
public int getRowCount(){
return gridContent.length;
}
public int getColumnCount(){
return gridContent[0].length;
}
public Object getItem(int rowNum, int columnNum) {
return gridContent[rowNum][columnNum];
}
public View getView(int position, View view, ViewGroup group) {
ImageView imageView;
if(view == null){
imageView = new ImageView(context);
}
else{
imageView = (ImageView) view;
}
columnPosition = position % gridContent[0].length;
rowPosition = (position - columnPosition)/gridContent[0].length;
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//Just Setting Up the Initial State of the Board Here
if(columnPosition%2 != 0 && rowPosition == 6){
setRed(imageView);
} else if(columnPosition%2 == 0 && (rowPosition ==5 || rowPosition==7)){
setRed(imageView);
}
else if(columnPosition%2 != 0 && (rowPosition == 0||rowPosition==2)){
setBlack(imageView);
} else if(columnPosition%2 == 0 && rowPosition == 1){
setBlack(imageView);
}
return imageView;
}
public Object getItem(int position){
return null;
}
public long getItemId(int position) {
return 0;
}
}