Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shuffle Array images in android

I am developing an Android application where I need to shuffle images that are stored in an array using a random function before assigning to an adapter. I have the following code on how to shuffle the array images, but it encounters an error

MainActivity.java:

public class MainActivity extends Activity {
    Context ctx;
    boolean flage=false;
    int img1=-1,img2=-1;
    public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        shuffleArray();
        final GridView grid = (GridView) findViewById(R.id.gv_memory);
        grid.setAdapter(new GridViewContent(this));
    }
    private void shuffleArray() {
        // TODO Auto-generated method stub
        Random arrayRandom= new Random();
        for(int i=0; i<OriginalArray.length-1; i++){
            int index=arrayRandom.nextInt(i);
            OriginalArray[i]=OriginalArray[index];

        }

    }
    public class GridViewContent extends BaseAdapter {
        private Context context;

        public int pictureArray[]={
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,        
        };
        public GridViewContent(Context c){
            context=c;

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return (pictureArray.length);
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return pictureArray[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(final int position, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            final ImageView myimage=new ImageView(context);
            myimage.setImageResource(pictureArray[position]);
            myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            myimage.setLayoutParams(new GridView.LayoutParams(70, 70));
            myimage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //Toast.makeText(context, String.valueOf(position), Toast.LENGTH_SHORT).show();
                myimage.setImageResource(OriginalArray[position]);

                if(flage==false)
                {

                img1=OriginalArray[position];

                flage=true;

                }else if(flage==true){

                img2=OriginalArray[position];
                checkResult();
                flage=false;
                }
                //else if(f)
                }
            });

            return myimage;
        }

    }
    public void checkResult() {
        if(img1==img2)
        {
            Toast.makeText(MainActivity.this, "Congratualatin !!!!", Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(MainActivity.this, "Sorry!!!!", Toast.LENGTH_LONG).show();
            final GridView grid = (GridView) findViewById(R.id.gv_memory);
            grid.setAdapter(new GridViewContent(this));
        }

    }
}     

LogCat......

03-12 05:40:31.904: D/dalvikvm(1081): GC_FOR_ALLOC freed 58K, 8% free 2771K/3000K, paused 198ms, total 201ms
03-12 05:40:31.914: I/dalvikvm-heap(1081): Grow heap (frag case) to 3.943MB for 1127536-byte allocation
03-12 05:40:31.965: D/dalvikvm(1081): GC_FOR_ALLOC freed 2K, 6% free 3870K/4104K, paused 47ms, total 47ms
03-12 05:40:32.014: D/AndroidRuntime(1081): Shutting down VM
03-12 05:40:32.014: W/dalvikvm(1081): threadid=1: thread exiting with uncaught exception (group=0x41465700)
03-12 05:40:32.024: E/AndroidRuntime(1081): FATAL EXCEPTION: main
03-12 05:40:32.024: E/AndroidRuntime(1081): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memoryforkids/com.example.memoryforkids.MainActivity}: java.lang.IllegalArgumentException: n <= 0: 0
03-12 05:40:32.024: E/AndroidRuntime(1081):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at android.os.Looper.loop(Looper.java:137)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at java.lang.reflect.Method.invoke(Method.java:525)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at dalvik.system.NativeStart.main(Native Method)
03-12 05:40:32.024: E/AndroidRuntime(1081): Caused by: java.lang.IllegalArgumentException: n <= 0: 0
03-12 05:40:32.024: E/AndroidRuntime(1081):     at java.util.Random.nextInt(Random.java:175)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at com.example.memoryforkids.MainActivity.shuffArray(MainActivity.java:47)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at com.example.memoryforkids.MainActivity.onCreate(MainActivity.java:38)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at android.app.Activity.performCreate(Activity.java:5133)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-12 05:40:32.024: E/AndroidRuntime(1081):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-12 05:40:32.024: E/AndroidRuntime(1081):     ... 11 more     
like image 552
Zia Ur Rehman Avatar asked Jun 21 '26 15:06

Zia Ur Rehman


1 Answers

First you need to convert you array into arraylist and then you can apply Collection.shuffle() on it.

Here below lines convert your int array of pictures into arraylist of Integer.

List<Integer> pictures = new ArrayList<Integer>();
for (int index = 0; index < OriginalArray.length; index++)
{
    pictures.add(OriginalArray[index]);
}

Then use Collection.shuffle() on it.

Collections.shuffle(pictures);

And now in your getView method you can use it by following way.

myimage.setImageResource(pictures.get(position));
like image 167
dakshbhatt21 Avatar answered Jun 24 '26 04:06

dakshbhatt21