0

Okay so the following is my ParseQuery. How can I display most but not all of the records in random order using Collections.shuffle() in this context? For example, I have a specific record that I want always to display at the top of the list but I want everything other than that specific record to be displayed in random order beneath it.

I'd prefer not to set up two different ArrayLists, displaying the one specific record in one and the rest in the other, but that is always an option.

Can I do something like removing the specific record from the shuffle based on its objectId somehow?

brandlist = new ArrayList<SuggestedBrand>();
        try {
            // Locate the class table named "SuggestedUser" in Parse.com
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                    "SuggestedBrand");

            ob = query.find();
            Collections.shuffle(ob);
            for (ParseObject author : ob) {
                ParseFile image = (ParseFile) author.get("brandImage");
                SuggestedBrand map = new SuggestedBrand();
                map.setRank((String) author.get("author"));
                map.setUsername((String) author.get("username"));
                map.setFlag(image.getUrl());
                map.setUserID((String) author.get("userId"));
                brandlist.add(map);
            }
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
Manuel
  • 14,274
  • 6
  • 57
  • 130

2 Answers2

1

Why don't you just remove the one interesting item (and store it in a variable) just before using Collections.shuffle(...), and after using the method insert the item back into the ArrayList at the top of it?

Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • I got it working, but how would I set it so the interesting item is at the top of the list (see answer below)? – drearypanoramic Oct 18 '15 at 00:00
  • 1
    You can use [`add(int location, E object)`](http://developer.android.com/reference/java/util/List.html#add(int,%20E)) method of the `List`, with `location` argument set to `0`. – Bartek Lipinski Oct 18 '15 at 09:00
0

This takes the item of interest and separates it from the randomization process. It then places it at the top of the list:

@Override
        protected Void doInBackground(Void... params) {
            // Create the array
            worldpopulationlist = new ArrayList<SuggestedUser>();
            try {
                final ParseQuery<ParseObject> interesting = new ParseQuery<ParseObject>("SuggestedUser");
                interesting.whereEqualTo("userId", "pYYMsL9RWW");
                ParseObject interesting2 = interesting.getFirst();
                // Locate the class table named "SuggestedUser" in Parse.com
                final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                        "SuggestedUser");
                ob = query.find();
                ob.remove(interesting2);
                Collections.shuffle(ob);
                ob.add(interesting2);
                Collections.reverse(ob);
                for (ParseObject author : ob) {
                    ParseFile image = (ParseFile) author.get("brandImage");
                    SuggestedUser map = new SuggestedUser();
                    map.setRank((String) author.get("author"));
                    map.setUsername((String) author.get("username"));
                    map.setFlag(image.getUrl());
                    map.setUserID((String) author.get("userId"));
                    worldpopulationlist.add(map);
                }
            } catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }