Remove All Ads from XDA
Honor 9 Lite

[Q] How To: Randomize a list??

613 posts
Thanks Meter: 32
By pramathesh, Senior Member on 3rd July 2014, 06:10 PM
Post Reply Email Thread
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.
3rd July 2014, 08:30 PM |#2  
Junior Member
Flag Wrocław
Thanks Meter: 1
Donate to Me
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();
4th July 2014, 09:51 AM |#3  
deanwray's Avatar
Senior Member
Thanks Meter: 429
Donate to Me
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
15th July 2014, 02:55 PM |#4  
Junior Member
Thanks Meter: 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
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes