Android中的Shuffle Array图像

时间:2014-03-12 10:04:15

标签: android gridview random

我正在开发一个Android应用程序,我需要在分配到适配器之前使用随机函数对存储在数组中的图像进行随机播放。 我有关于如何对数组图像进行混洗的以下代码,但它遇到错误

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     

1 个答案:

答案 0 :(得分:2)

首先,您需要将数组转换为arraylist,然后才能在其上应用Collection.shuffle()

以下行将您的int图片数组转换为Integer的arraylist。

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

然后在其上使用Collection.shuffle()

Collections.shuffle(pictures);

现在,在您的getView方法中,您可以按照以下方式使用它。

myimage.setImageResource(pictures.get(position));