• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps weird random shuffle problem

hi guys - first post whoo! :)
i'm a flash developer who's just starting to learn android development. my first app is a simple kid's flash card app - basically a gallery of images with accompanying sounds. i have the gallery set up and sounds play fine. i want the gallery to shuffle to a random order on each launch. i have this working fine, using this method, which gets called in the onCreate function:

Code:
static void shuffleArray(Integer[] ar)
    {
      Random rnd = new Random();
      for (int i = ar.length - 1; i >= 0; i--)
      {
        int index = rnd.nextInt(i + 1);
        // Simple swap
        int a = ar[index];
        int b = mSoundIds[index];
        ar[index] = ar[i];
        mSoundIds[index] = mSoundIds[i];
        ar[i] = a;
        mSoundIds[i] = b;
      }
    }

this shuffles the arrays (one which i'm passing in of the imageIds and the other is the sounds which need to maintain the same order as the images of course) - i know this isn't the best way to do this, but it works. initially. when i relaunch the app and the shuffle happens again, the images and audio don't match up, and i'm wondering why! any clues?

or better yet, i am used to using objects in flash and were this a flash movie, i'd be using objects to link the images with the sounds... anything like that exist in java that i can use instead of this messy workaround?

appreciate the help!

emma.
 
not sure if I understood correctly , but in java you can define a class that would contain your images and sounds

and to figure out what was exactly wrong , try to debug and see what is happening exactly
 
Yep JiMMaR has the right solution, you should be putting these two related peices of data into an object together. I'm a Flash/Flex convert myself and in Flex these are called Value Ojects. In Java they are called Data Transfer Objects(DTO) or Plain Old Java Objects (POJO). (Java also has something called a Value Object but it's more related to enum values and kinda off-topic.)

Anyways here's what your object might look like:

Code:
public class FlashCardObject 
{
    public int audioID  = -1;
    public int imageID = -1;

    // you can also make the field private and use getters and setters if you 
    // wish, I prefer public variables, but it's a matter of style *mostly*
}
Pretty simple, eh?

Now you can use an ListAdapter/ArrayAdapter (like a Model from the MVC design pattern) to hold your data.

Code:
// (VERY OVER SIMPLFIED)
import java.util.ArrayList;
import android.widget.BaseAdapter;
import com.yourdomain.yourapp.FlashCardObject

public class FlashcardAdapter extends BaseAdapter
{
    private ArrayList<FlashCardObject> list=new ArrayList<FlashCardObject>();

    public FlashcardAdapter () 
    {
        
    }

    public void addItem( FlashCardObject obj )
    {
        list.add ( obj );
    }

    public void shuffleList()
    {
        //put code here to randomize/shuffle the list
    }

}
While the second part is over-simplfied, the important thing to not is that now you are randomizing the objects and you dont have to keep track of two separate int[] arrays (which are pretty inflexible).
 
Back
Top Bottom