我有一个Recipe
实现的对象Comparable<Recipe>
:
public int compareTo(Recipe otherRecipe) {
return this.inputRecipeName.compareTo(otherRecipe.inputRecipeName);
}
Run Code Online (Sandbox Code Playgroud)
我已经这样做了所以我能够List
按以下方法按字母顺序排序:
public static Collection<Recipe> getRecipes(){
List<Recipe> recipes = new ArrayList<Recipe>(RECIPE_MAP.values());
Collections.sort(recipes);
return recipes;
}
Run Code Online (Sandbox Code Playgroud)
但是现在,在另一种方法中,让我们调用它getRecipesSort()
,我想要对数据进行排序,但数字,比较包含其ID的变量.更糟糕的是,ID字段属于类型String
.
如何使用Collections.sort()在Java中执行排序?
我正在尝试在我的应用中设置Android服务,以便在Firebase Ref中侦听新的Child,并在发生这种情况时抛出通知.
我遇到了问题,因为addChildEventListener onChildAdded
显然每次存在的记录都会被调用一次,然后实际上只会听新的孩子.
在这个答案中, @ kato说如果addChildEventListener
被调用就像ref.endAt().limit(1).addChildEventListener(...)
只获得新添加的记录一样.
它实际上一次只能获得一条记录(我猜limit(1)
)但在收听添加的记录之前它仍然会获得一条记录.
这是一些代码:
在onCreate()中初始化监听器:
@Override
public void onCreate() {
super.onCreate();
this.handler = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
AllowedGroup ag = dataSnapshot.getValue(AllowedGroup.class);
postNotif("Group Added!", ag.getName());
}
...rest of needed overrides, not used...
Run Code Online (Sandbox Code Playgroud)
我正在使用它AllowedGroup.class
来存储记录,以及postNotif
构建和发布通知.这部分按预期工作.
然后,onStartCommand()
:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.f = new Firebase(FIREBASE_URL).child("users").child(this.currentUserUid).child("allowedGroups");
f.endAt().limit(1).addChildEventListener(handler);
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)
在实际收听新添加的孩子之前,它仍会返回一个存在的 …
我正在尝试使用JSP页面开发Spring MVC应用程序,但我遇到了一个问题.它更像是一个创造性问题,而不是一个代码问题,但是这里有:
因此,应用程序基本上收到一个配方(字段名称,问题描述,问题解决方案等),并在创建时在其上拍一个ID.
我想要的是在头版上显示最后创建的3个食谱.我想出了一个显然显示前 3个食谱的代码:
<c:forEach var="recipe" items='${recipes}'>
<c:if test="${recipe.id < 4}
<div class="span4">
<h3<c:out value="${recipe.inputDescProb}"></c:out></h3>
<p><c:out value="${recipe.inputDescSol}"></c:out></p>
<p><a class="btn" href="/recipes/${recipe.id}">Details »</a></p>
</div>
</c:if>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
有关如何显示最后 3个食谱的想法吗?