Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,808,781 Members 46,484 Now Online
XDA Developers Android and Mobile Development Forum

[Q] How To: Randomize a list??

Tip us?
Senior Member - OP
Thanks Meter 26
Posts: 554
Join Date: Oct 2010
Location: Mumbai
Question [Q] How To: Randomize a list??

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?
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.

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.
Someone please suggest something nice for a sig!
Junior Member
Thanks Meter 1
Posts: 12
Join Date: Jun 2014
Location: Wrocław

Look on the animations tutorial at ( 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();
(Last edited by deanwray; 4th July 2014 at 08:53 AM.)
deanwray's Avatar
Senior Member
Thanks Meter 418
Posts: 1,121
Join Date: Apr 2006

I think this would be the easiest and most explanatory way that you could understand for the random list
    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());

        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
Junior Member
Thanks Meter 3
Posts: 13
Join Date: Jul 2014
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes