Sunday, 11 August 2013

Need Help Using a 2-Dimensional Array Adapter

Need Help Using a 2-Dimensional Array Adapter

So I made this custom 2d array adapter(basically copied it from someone
else, so I don't completely know what I am doing with it), because I am
making a checkers board game, and I wanted the board to be a 2d array of
imageviews. I'm just wondering: How do I actually access each individual
item in this 2d array in my main activity. I know in the OnClick it gives
me both the view that is selected and the position, but I was just
wondering if there is any way that I can do something like
array[row][column].setBackgroundResource(R.drawable.piecered2) in my main
activity.
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;
}
}

No comments:

Post a Comment