know where clicked gridview android -
i new android, , i'm developing game memorized. using gridview display images. have arraylist pictures (animals) , arraylist images (incognita). able when click on image display animal, , if backs equal, if not match can hide clickeo last picture. achievement esconnder not first image clicked. how can know elements of gridview pushed ?.
greetings , thanks.
public class juego3libre extends activity implements 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 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 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 image in touched position , setup image imageview.setimageresource(animaleslist.get(position)); comparar[contador] = animaleslist.get(position); contador=contador+1; temporal[contador2] = animaleslist.get(position); contador2=contador2+1; //clean test score textview tv = (textview)findviewbyid(r.id.numeropuntaje); tv.settext(" "); //i compare images touched while(contador==2){ if(comparar[0]==comparar[1]){ //set text see if comparisons correct tv.settext("iguales"); contador=0; contador2=0; } if(comparar[0]!=comparar[1]){ //set text see if compareciones incorrect tv.settext("diferentes"); //i try hide images not same imageview.setimageresource(signolist.get(temporal[0])); imageview.setimageresource(signolist.get(temporal[1])); contador=0; contador2=0; } } }
}
Comments
Post a Comment