1

Hi friends i am having two arraylists one is image arraylist and another one is sound arraylist,i have to give background to imageviews randomly using image arrylist,and when i click that image i need to play that image related sound,both arraylists am shffling now, i can set image background randomly,but my problem is in sound,after shuffling sound is play wrong if 'a' image means its playing 'c' sound and some times playing b sounds etc..how to solve this any one suggest me

 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();
    }

}

}
user2401554
  • 121
  • 4
  • 19

2 Answers2

2

Create a Pair of picture and music (or better your own class), put them into List and shuffle it.

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. You have a lot of duplicate code. For example, add Pair to imageView as tag.

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

Then in clickListener:

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

So you don need to copy-paste code same code for each imageView.

Community
  • 1
  • 1
Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • Exactly, create your own class that has both image and sound, and make the arrayList of this class instead of making two different arrayLists. – Abhishek Shukla Jul 23 '13 at 07:09
  • Pair pict = pics.get(0); But instead of Pair you better use your class. Pair is too general. Create something like Holder (but name it wisely) from @Suji answer. – Leonidos Jul 23 '13 at 07:32
  • hi Leonidos thakyou for single image its working fine in sound also,can you tel me how i have to add 2 image and second sound in that pics arraylist – user2401554 Jul 23 '13 at 07:36
  • pics.add(new Pair(R.drawable.b_white, R.raw.b)); image2.setBackgroundResource(pics.get(1).first) You can add imageViews to an array and loop throught them, so you wont need to write imageXX.setBackgroundResource(pics.get(YY).first) code 6 times. – Leonidos Jul 23 '13 at 07:44
0

You can create an inner class for holding both string and sound value like this:private

class Holder{
    int stringVal;
    int soundVal;

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

}

Then declare Vector of Holder as follows:

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

Then do the copy stringvalues and soundvalues stringsAndSounds arraylist as follows:

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

Then do shuffle the stringsAndSounds list:

Collections.shuffle(stringsAndSounds);

Now you can access stringsAndSounds anywhere in a for loop as follows, which is already shuffled :

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

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

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

}
Suji
  • 6,044
  • 2
  • 19
  • 17
  • ArrayList is better than Vector in this case. – Leonidos Jul 23 '13 at 07:24
  • hi suji i tried using ur way like below Vector stringsAndSounds=new Vector(); for(int i=0;i – user2401554 Jul 23 '13 at 07:45
  • Collections.shuffle(stringsAndSounds); for(int i=0;i – user2401554 Jul 23 '13 at 07:46
  • but in array i have 2 repeted images always they displaying 1 after each other, – user2401554 Jul 23 '13 at 07:48
  • Once you shuffled the Vector using Collections.shuffle(stringsAndSounds), you don't need to go for sound1 and stringArray1. just use stringsAndSounds as follows in your onClick() method: if(v==image2) { if (filpsound == null) { filpsound = MediaPlayer.create(getApplicationContext(), stringsAndSounds[1].soundVal); } filpsound.start(); }. and so on. – Suji Jul 23 '13 at 09:01