I m trying to create a memory game - where I have 5x5 images on the screen and the user has to match the images. I ve been using a GridView and populate it with images using a ImageAdapter. The game works something like this: - when a user matches 2 images - the images remain on the screen - when a user fails to match the 2 pictures - the imagess changes back to the question-mark. The problem is that I can t manage to keep the pictures previously matched on the screen - when I use the notifyDataSetChanged() method - all the screen is filled again with question marks. Here s my code:
// 在图像编辑器中获取查看方法
public View getView(int position, View convertView, ViewGroup arg2) {
ImageView imageView;
if (convertView == null) { // if it s not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
} else {
imageView = (ImageView) convertView;
}
for(int i=0;i<16;i++)
{
if(mThumbIds[i].equals(R.drawable.ic_launcher))
{
imageView.setImageResource(R.drawable.ic_launcher);
}
else
imageView.setImageResource(R.drawable.q_mark);
}
return imageView;
}
/ 用户选择 1 张图像时的点击听器
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
i++;
Toast.makeText(easyGame.this, "" + position, Toast.LENGTH_SHORT).show();
ImageView imgV=(ImageView)v;
if(i%2!=0)
{
firstClick=position;
imgV.setImageResource(ImageAdapter.mThumbIds[firstClick]);
}
else
{
secondClick=position;
imgV.setImageResource(ImageAdapter.mThumbIds[secondClick]);
}
if(i%2==0)
{
if(!(ImageAdapter.mThumbIds[firstClick].equals(ImageAdapter.mThumbIds[secondClick])))
{
Toast.makeText(easyGame.this, "Great!", Toast.LENGTH_SHORT).show();
ImageAdapter.mThumbIds[firstClick]=ImageAdapter.mThumbsIdsDone[0];
ImageAdapter.mThumbIds[secondClick]=ImageAdapter.mThumbsIdsDone[0];
im.notifyDataSetChanged();
gridview.setAdapter(im);
gridview.invalidate();
}
}
}
});
有人能帮忙吗?