我是android新手,我目前正在开发记忆中的游戏。我正在使用gridview来显示图像。我有一个带图片(动物)的arraylist和另一个带图像的arraylist(incognita)。 当我点击图像时,我能够看到我展示了一只动物,如果他们的背部是相同的,但如果它们不匹配,我可以隐藏clickeo的最后一张照片。成就esconnder不是第一个被点击的图像。我怎么知道我推动的gridview的元素?
问候和感谢。
public class Juego3Libre extends Activity实现android.widget.AdapterView.OnItemClickListener {
private ArrayList<Integer> animaleslist = new ArrayList<Integer>();
private ArrayList<Integer> signolist = new ArrayList<Integer>();
int[] comparar = new int[2];
int[] temporal = new int[2];
int contador=0,contador2=0;
ImageView imgv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.juego3libre);
//Acá seteo el nombre del layout
this.setTitle("JUEGO MEMORICE");
GridView gridview = (GridView) findViewById(R.id.GridView);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(this);
}
public class ImageAdapter extends BaseAdapter{
private Context contexto;
public ImageAdapter(Context c){
contexto=c;
//thumbnails are loaded
animaleslist.add(R.drawable.caballo);
animaleslist.add(R.drawable.cerdo);
animaleslist.add(R.drawable.leon);
animaleslist.add(R.drawable.rana);
animaleslist.add(R.drawable.gato);
animaleslist.add(R.drawable.gallo);
animaleslist.add(R.drawable.foca);
animaleslist.add(R.drawable.mono);
animaleslist.add(R.drawable.caballo);
animaleslist.add(R.drawable.cerdo);
animaleslist.add(R.drawable.leon);
animaleslist.add(R.drawable.rana);
animaleslist.add(R.drawable.gato);
animaleslist.add(R.drawable.gallo);
animaleslist.add(R.drawable.foca);
animaleslist.add(R.drawable.mono);
Collections.shuffle(animaleslist);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
//
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
signolist.add(R.drawable.signointe);
}
@Override
//Number of images to be displayed
public int getCount() {
// TODO Auto-generated method stub
return animaleslist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageview;
if(convertView == null){
imageview = new ImageView(contexto);
imageview.setLayoutParams(new GridView.LayoutParams(85,85));
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setPadding(5, 5, 5, 5);
}else{
imageview = (ImageView) convertView;
}
//imageview.setImageResource(animaleslist.get(position));
imageview.setImageResource(signolist.get(position));
return imageview;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
ImageView imageview =(ImageView) v;
//I get the image in the touched position and setup image
imageview.setImageResource(animaleslist.get(position));
comparar[contador] = animaleslist.get(position);
contador=contador+1;
temporal[contador2] = animaleslist.get(position);
contador2=contador2+1;
//Clean the test score
TextView tv = (TextView)findViewById(R.id.numeropuntaje);
tv.setText(" ");
//I compare the images touched
while(contador==2){
if(comparar[0]==comparar[1]){
//Set text to see if the comparisons are correct
tv.setText("iguales");
contador=0;
contador2=0;
}
if(comparar[0]!=comparar[1]){
//set text to see if compareciones are incorrect
tv.setText("diferentes");
//I try to hide the images that are not the same
imageview.setImageResource(signolist.get(temporal[0]));
imageview.setImageResource(signolist.get(temporal[1]));
contador=0;
contador2=0;
}
}
}
}