如何在不改变两个arraylists的顺序的情况下混洗两个不同的数组列表值

时间:2013-07-23 06:45:54

标签: android android-emulator

嗨朋友我有两个arraylists,一个是图像arraylist,另一个是声音arraylist,我必须使用图像arrylist随机给出imageview的背景,当我点击该图像我需要播放该图像相关的声音时,两个arraylists现在,我可以随意设置图像背景,但我的问题在于声音,如果'a'图像意味着播放'c'声音并且有时播放b声音等等,则播放声音播放错误。如何解决这个问题一个人建议我

 public class MainActivity extends Activity implements OnClickListener {

float screenHeight, screenWidth, screendensity;
ImageView image1,image2,image3,image4,image5,image6;
static ArrayList<Integer> sltdalphabet=new ArrayList<Integer>();
static ArrayList<Integer> sounds=new ArrayList<Integer>();
Integer[] stringArray1;
Integer[] soundarray;
MediaPlayer filpsound;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        screenHeight = displaymetrics.heightPixels;
        screenWidth = displaymetrics.widthPixels;
        screendensity = displaymetrics.densityDpi;
    setContentView(R.layout.flipcaps);

    sounds.clear();
    sltdalphabet.clear();

    sltdalphabet.add(R.drawable.a_white);
    sltdalphabet.add(R.drawable.b_white);
    sltdalphabet.add(R.drawable.c_white);
    sltdalphabet.add(R.drawable.d_white);
    sltdalphabet.add(R.drawable.e_white);
    sltdalphabet.add(R.drawable.f_white);

    sounds.add(R.raw.a);
    sounds.add(R.raw.b);
    sounds.add(R.raw.c);
    sounds.add(R.raw.d);
    sounds.add(R.raw.e);
    sounds.add(R.raw.f);

    image1=(ImageView)findViewById(R.id.img1);
    image1.setOnClickListener(this);
    RelativeLayout.LayoutParams layoutflipbtn1copy = (RelativeLayout.LayoutParams) image1
            .getLayoutParams();
    layoutflipbtn1copy.height = (int) (170 * (screenHeight / 600));
    layoutflipbtn1copy.width = (int) (170 * (screenWidth / 1024));
    layoutflipbtn1copy.topMargin = (int) (120 * (screenHeight / 600));
    layoutflipbtn1copy.leftMargin = (int) (240 * (screenWidth / 1024));


    image2=(ImageView)findViewById(R.id.img2);
    image2.setOnClickListener(this);
    RelativeLayout.LayoutParams layoutflipbtn2copy = (RelativeLayout.LayoutParams) image2
            .getLayoutParams();
    layoutflipbtn2copy.height = (int) (170 * (screenHeight / 600));
    layoutflipbtn2copy.width = (int) (170 * (screenWidth / 1024));
    layoutflipbtn2copy.topMargin = (int) (120 * (screenHeight / 600));
    layoutflipbtn2copy.leftMargin = (int) (430 * (screenWidth / 1024));


    image3=(ImageView)findViewById(R.id.img3);
    image3.setOnClickListener(this);
    RelativeLayout.LayoutParams layoutflipbtn3copy = (RelativeLayout.LayoutParams) image3
            .getLayoutParams();
    layoutflipbtn3copy.height = (int) (170 * (screenHeight / 600));
    layoutflipbtn3copy.width = (int) (170 * (screenWidth / 1024));
    layoutflipbtn3copy.topMargin = (int) (120 * (screenHeight / 600));
    layoutflipbtn3copy.leftMargin = (int) (620 * (screenWidth / 1024));

    image4=(ImageView)findViewById(R.id.img4);
    image4.setOnClickListener(this);
        RelativeLayout.LayoutParams layoutflipbtn4copy = (RelativeLayout.LayoutParams) image4
                .getLayoutParams();
        layoutflipbtn4copy.height = (int) (170 * (screenHeight / 600));
        layoutflipbtn4copy.width = (int) (170 * (screenWidth / 1024));
        layoutflipbtn4copy.topMargin = (int) (300 * (screenHeight / 600));
        layoutflipbtn4copy.leftMargin = (int) (240 * (screenWidth / 1024));

        image5=(ImageView)findViewById(R.id.img5);
        image5.setOnClickListener(this);
            RelativeLayout.LayoutParams layoutflipbtn5copy = (RelativeLayout.LayoutParams) image5
                    .getLayoutParams();
            layoutflipbtn5copy.height = (int) (170 * (screenHeight / 600));
            layoutflipbtn5copy.width = (int) (170 * (screenWidth / 1024));
            layoutflipbtn5copy.topMargin = (int) (300 * (screenHeight / 600));
            layoutflipbtn5copy.leftMargin = (int) (430 * (screenWidth / 1024));

            image6=(ImageView)findViewById(R.id.img6);
            image6.setOnClickListener(this);
                RelativeLayout.LayoutParams layoutflipbtn6copy = (RelativeLayout.LayoutParams) image6
                        .getLayoutParams();
                layoutflipbtn6copy.height = (int) (170 * (screenHeight / 600));
                layoutflipbtn6copy.width = (int) (170 * (screenWidth / 1024));
                layoutflipbtn6copy.topMargin = (int) (300 * (screenHeight / 600));
                layoutflipbtn6copy.leftMargin = (int) (620 * (screenWidth / 1024));

                  Collections.shuffle(sltdalphabet);
                  Collections.shuffle(sounds);

                 stringArray1 = sltdalphabet.toArray(new Integer[sltdalphabet.size()]);
                 soundarray = sounds.toArray(new Integer[sounds.size()]);


                image1.setBackgroundResource(stringArray1[0]);
                image2.setBackgroundResource(stringArray1[1]);
                image3.setBackgroundResource(stringArray1[2]);
                image4.setBackgroundResource(stringArray1[3]);
                image5.setBackgroundResource(stringArray1[4]);
                image6.setBackgroundResource(stringArray1[5]);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if(v==image1)
    {
        if (filpsound == null) {
            filpsound = MediaPlayer.create(getApplicationContext(),
                    soundarray[0]);
        }
        filpsound.start();
    }
    else if(v==image2)
    {
        if (filpsound == null) {
            filpsound = MediaPlayer.create(getApplicationContext(),
                    soundarray[1]);
        }
        filpsound.start();
    }

    else if(v==image3)
    {
        if (filpsound == null) {
            filpsound = MediaPlayer.create(getApplicationContext(),
                    soundarray[2]);
        }
        filpsound.start();
    }
    else if(v==image4)
    {
        if (filpsound == null) {
            filpsound = MediaPlayer.create(getApplicationContext(),
                    soundarray[3]);
        }
        filpsound.start();
    }   
    else if(v==image5)
    {
        if (filpsound == null) {
            filpsound = MediaPlayer.create(getApplicationContext(),
                    soundarray[4]);
        }
        filpsound.start();
    }
    else if(v==image6)
    {
        if (filpsound == null) {
            filpsound = MediaPlayer.create(getApplicationContext(),
                    soundarray[5]);
        }
        filpsound.start();
    }

}

}

2 个答案:

答案 0 :(得分:2)

创建Pair图片和音乐(或者更好的自己的类),将它们放入List并随机播放。

List<Pair<Integer, Integer>> pics = new ArrayList<Pair<Integer, Integer>>();
pics.add(new Pair<Integer, Integer>(R.drawable.a_white, R.raw.a));
...
Collections.shuffle(pics);

image1.setBackgroundResource(pics.get(0).first)

P.S。你有很多重复的代码。例如,将Pair添加到imageView作为标记。

pict = pics.get(0);
image1.setBackgroundResource(pict.first);
image1.setTag(pict);

然后在clickListener中:

Pair<Integer, Integer> pict = (Pair<Integer, Integer>)v.getTag();
...
filpsound = MediaPlayer.create(getApplicationContext(), pict.second)

因此,您不需要为每个imageView复制粘贴代码相同的代码。

答案 1 :(得分:0)

您可以创建一个内部类来保存字符串和声音值,如下所示:private

class Holder{
    int stringVal;
    int soundVal;

    public Holder(int stringVal,int soundVal) {
        this.stringVal=stringVal;
        this.soundVal=soundVal;
    }

}

然后按如下方式声明持有人Vector:

Vector<Holder> stringsAndSounds=new Vector<Holder>();

然后按如下方式复制stringvalues和soundvalues stringsAndSounds arraylist:

 for(int i=0;i<sltdalphabet.size() && i<sounds.size();i++){
     stringsAndSounds.add(new Holder(sltdalphabet.get(i), sounds.get(i)));
 }

然后将hashAndSounds列表随机播放:

Collections.shuffle(stringsAndSounds);

现在你可以在for循环中的任何地方访问stringsAndSounds,如下所示,已经改组:

for(int i=0;i<stringsAndSounds.size();i++){

int stringVal = stringsAndSounds.elementAt(0).stringVal;

int soundVal = stringsAndSounds.elementAt(0).soundVal;
// do your code

}