Post Reply
Question

[Q] How To: Randomize a list??

3rd July 2014, 05:10 PM   |  #1  
OP Senior Member
Flag Mumbai
Thanks Meter: 26
 
554 posts
Join Date:Joined: Oct 2010
More
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.
3rd July 2014, 07:30 PM   |  #2  
Junior Member
Flag Wrocław
Thanks Meter: 1
 
14 posts
Join Date:Joined: Jun 2014
Donate to Me
More
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();
4th July 2014, 08:51 AM   |  #3  
deanwray's Avatar
Senior Member
Thanks Meter: 418
 
1,121 posts
Join Date:Joined: Apr 2006
Donate to Me
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 by deanwray; 4th July 2014 at 08:53 AM.
15th July 2014, 01:55 PM   |  #4  
Junior Member
Thanks Meter: 3
 
13 posts
Join Date:Joined: 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
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Top Threads in Java for Android App Development by ThreadRank