java - Shuffling Gridview items without repeats tried many approaches -


i may losing live. have been trying write method shuffle images on gridview uses imageadapter. inexperienced @ android. have accessed questions on site relating shuffling arrays no repeats. have tested them in normal java environment. there ones work no repeats. have tried changing array arraylist , used collections.shuffle. still repeats or emulator has crashed. appreciate insight on this- it's simple cannot it. here of methods (using code have found).

   public integer[] myshuffles(integer []ji){      arraylist<integer> numbers = new arraylist<integer>();        random randomgenerator = new random();     while (numbers.size() < 36) {          int random = randomgenerator.nextint(35);         if (!numbers.contains(random)) {             numbers.add(random);          }      }       (int i=0; i<ji.length;i++) {          ji[i] =ji[numbers.get(i)] ;     //  i++;    }     return ji; } 

another

        static int[] shufflearray(int[] ar) {   random rnd = new random();  int randomno = rnd.nextint()*3; //  int randomno = (int)math.floor(math.random() * 3);   (int = ar.length - 1; >= 0; i--)   {   //  int index = rnd.nextint(i + 1);       int index = randomno - 1;     // simple swap     int = ar[index];     ar[index] = ar[i];     ar[i] = a;   }    return ar; }   public static void shuffleaarray(int[] a) {    int n = a.length;    int random = randomint(0,35); //  random.nextint();     (int = 0; < n; i++) {         int change = + random;        swap(a, i, change);    } } 

another

  public integer[] myshuffle( integer[]jigx){       boolean found = false;  (int = 0; < jigx.length; i++) {       {          found = false;       int  temp = randomint(0,35);       jigx[i] = temp;         (int j = 0; j < && !found; j++) {           if (jigx[j] == temp)               found = true;         }        }        while (found);        }      return jigx;     }  

also tried collections.shuffle , put in next piece images

    myshuffles(jigx);//or whatever method  imageview.setimageresource(jigx [position]); 

if can wirh great! thanks

ok, after forth , back, looks problem not shuffling itself. guess shuffle more once, why duplicate entries. make custom listadapter can shuffle items.

public class myimageadapter extends baseadapter {     private context mctx;     private list<integer> mimages;      public myimageadapter(final context ctx, final list<integer> images) {         mctx = ctx;         mimages = images;     }      public void shuffle() {         collections.shuffle(mimages, new random(system.currenttimemillis()));     }      @override     public int getcount() {         return mimages.size();     }      @override     public object getitem(final int position) {         return mimages.get(position);     }      @override     public long getitemid(final int position) {         return 0;     }      @override     public view getview(final int position, final view convertview, final viewgroup parent) {         imageview iv = (imageview) convertview;         if (iv == null) {             iv = new imageview(mctx);         }          iv.setimageresource(mimages.get(position));          return iv;     } } 

in activity use follows:

@override protected void oncreate(final bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.my_layout);      list<integer> images = new arraylist<integer>();     images.add(r.drawable.image1);     images.add(r.drawable.image2);     images.add(r.drawable.image3);     ...     gridview gv = (gridview) findviewbyid(r.id.grid_view);     madapter = new myimageadapter(this, images);     gv.setadapter(madapter);     ...         madapter.shuffle();     madapter.notifydatasetchanged(); } 

is looking for?


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -