Skip to content
Advertisement

Displaying some query records in random order (Parse.com)

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;
    }

Advertisement

Answer

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?

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement