Reputation: 465
I'm building a simple floodit game in android, and am struggling to make the gridview or rather the "ImageAdapterGridView" update the image on click. When a user clicks on a cell, the game registers the colour of the cell and makes the top left cell the same colour. The issue being I am unable to make the grid redraw. All the solutions I have found online seem to be populating the grid in a different way to me.
public class GameActivity extends AppCompatActivity {
public int clickedCell;
public String colour = "error";
public int[] numbers = new int[64];
public Context mContext;
public GridView androidGridView;
public Integer[] imageIDs = {
R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
R.drawable.blue, R.drawable.orange, R.drawable.pink, R.drawable.purple, R.drawable.red, R.drawable.yellow, R.drawable.blue, R.drawable.orange,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Random random= new Random();
for(int i = 0; i < numbers.length; i++) {
int number= random.nextInt(6);
numbers[i]= number;
}
for(int i =0; i < numbers.length; i++) {
if(numbers[i]==0) {
imageIDs[i] = R.drawable.blue;
}
else if(numbers[i]==1) {
imageIDs[i] = R.drawable.orange;
}
else if(numbers[i]==2) {
imageIDs[i] = R.drawable.pink;
}
else if(numbers[i]==3) {
imageIDs[i] = R.drawable.purple;
}
else if(numbers[i]==4) {
imageIDs[i] = R.drawable.red;
}
else if(numbers[i]==5) {
imageIDs[i] = R.drawable.yellow;
}
}
androidGridView = (GridView) findViewById(R.id.gridview_android_example);
androidGridView.setAdapter(new ImageAdapterGridView(this));
androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View v, int position, long id) {
ImageAdapterGridView IAGV = new ImageAdapterGridView(mContext);
clickedCell = position;
if(numbers[clickedCell]==0) {
imageIDs[0] = R.drawable.blue;
colour = "blue";
Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected. It is " + colour, Toast.LENGTH_SHORT).show();
IAGV.getView(0, null, parent);
}
else if(numbers[clickedCell]==1) {
imageIDs[0] = R.drawable.orange;
colour = "orange";
Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected. It is " + colour, Toast.LENGTH_SHORT).show();
IAGV.getView(0, null, parent);
}
else if(numbers[clickedCell]==2) {
imageIDs[0] = R.drawable.pink;
colour = "pink";
Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected. It is " + colour, Toast.LENGTH_SHORT).show();
IAGV.getView(0, null, parent);
}
else if(numbers[clickedCell]==3) {
imageIDs[0] = R.drawable.purple;
colour = "purple";
Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected. It is " + colour, Toast.LENGTH_SHORT).show();
IAGV.getView(0, null, parent);
}
else if(numbers[clickedCell]==4) {
imageIDs[0] = R.drawable.red;
colour = "red";
Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected. It is " + colour, Toast.LENGTH_SHORT).show();
IAGV.getView(0, null, parent);
}
else if(numbers[clickedCell]==5) {
imageIDs[0] = R.drawable.yellow;
colour = "yellow";
Toast.makeText(getBaseContext(), "Tile " + (position + 1) + " Selected. It is " + colour, Toast.LENGTH_SHORT).show();
IAGV.getView(0, null, parent);
}
}
});
}
public class ImageAdapterGridView extends BaseAdapter {
public ImageAdapterGridView(Context c) {
mContext = c;
}
public int getCount() {
return imageIDs.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView mImageView;
if (convertView == null) {
mImageView = new ImageView(mContext);
mImageView.setLayoutParams(new GridView.LayoutParams(130, 130));
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mImageView.setPadding(1, 4, 1, 4);
} else {
mImageView = (ImageView) convertView;
}
mImageView.setImageResource(imageIDs[position]);
return mImageView;
}
}
}
This is my main class. Currently I am trying to re-call the getView() class in the hopes this will re-draw the images, but I don't know if this is possible. If any other info is needed let me know.
Thanks a lot.
Upvotes: 1
Views: 29
Reputation: 33544
Extract the adapter into a variable:
//// extract adapter into a variable instead of creating inline
// androidGridView.setAdapter(new ImageAdapterGridView(this));
ImageAdapterGridView adapter = new ImageAdapterGridView(this);
androidGridView.setAdapter(adapter);
Then you can call .notifyDataSetChanged
to refresh the view inside your onclick:
adapter.notifyDataSetChanged();
You can just do it once at the bottom of the onClick
method.
Also you were creating a second adapter inside the click handler:
ImageAdapterGridView IAGV = new ImageAdapterGridView(mContext);
which was creating a new adapter completely, so you would have two adapters at that point, but one was not attached to the view.
One other small thing is it's name is somewhat confusing, since it's an adapter but named as though it were a view. I would maybe name it ImageGridViewAdapter
.
Upvotes: 1