好的以下是我的ParseQuery。如何在此上下文中使用Collections.shuffle()以随机顺序显示大多数但不是所有记录?例如,我有一个特定的记录,我希望始终显示在列表的顶部,但我希望除了特定记录之外的所有内容都以随机顺序显示在它下面。
我不想设置两个不同的ArrayLists,在一个中显示一个特定记录,在另一个中显示其余记录,但这始终是一个选项。
我可以做一些事情,比如根据它的objectId以某种方式从shuffle中删除特定记录吗?
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;
}
答案 0 :(得分:1)
为什么不在使用Collections.shuffle(...)
之前删除一个有趣的项目(并将其存储在变量中),并在使用该方法后将项目重新插入顶部的ArrayList
它?
答案 1 :(得分:0)
这将获取感兴趣的项目并将其与随机化过程分开。然后它将它放在列表的顶部:
@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;
}