[Q] How To: Randomize a list??

Search This thread

pramathesh

Senior Member
Oct 25, 2010
666
40
Bangalore
Dear Friends,

I'm working on an application which has a list of 5 items connected to a database at the back end. I would like to know how can I randomize the order of appearance of these 5 items in these list every time the app start?
Ex
Item 1: A
Item 2: B
Item 3: C
Item 4: D
Item 5: E

Next time the app launches the order of appearance is at random.

Ex
Item 1: C
Item 2: E
Item 3: A
Item 4: D
Item 5: B

So, on and so forth for every instances of app launch.


Also, how can I add transition animation (fade in, fade out, slide in, etc) like most of the modern web-pages have for the app UI. I do not have much programming experience. Just a learner. All help will be appreciated.
 

panwrona

Member
Jun 6, 2014
15
1
Wrocław
Look on the animations tutorial at androidhive.info ( i think it's proper address ).

Answering the first question, I would write method for database that returns list of items and implement randomization in it. Then just create instance of database in onCreate and make just like this : List<Item> randomList = db.returnRandomList();
 

deanwray

Senior Member
Apr 2, 2006
1,145
427
www.deanwray.co.uk
I think this would be the easiest and most explanatory way that you could understand for the random list
Code:
    private ArrayList<String> random(ArrayList<String> list)
    {
        ArrayList<String> randomList = new ArrayList<String>();

        while(list.size() > 0)
        {
            int randomInt = new Random(System.currentTimeMillis()).nextInt(list.size());
            randomList.add(list.get(randomInt));
            list.remove(randomInt);
        }

        return randomList;
    }

So with that imagine doing it with "_id"'s before asking the cursor for rows (though you would need all the primary keys to start with to do that on)... hope that gives you enough to start with



as for anims thats part of the view class and much info on d.android.com
 
Last edited:

OkieKokie

Member
Jul 15, 2014
15
4
To randomize the list use Java's Collections. Shuffle method and pass in ur list.
In order to display animations, after u call startactivity, make a call to overridependingtransition and pass in animations for the entering and exiting activity.

Sent from my XT1022 using XDA Free mobile app