我目前正在使用Firebase作为数据库,并尝试使用orderbypriority对我的firebase条目进行排序。但是我遇到了orderbypriority的问题。当我在firebase引用上使用orderbypriority来查询我的数据库时,它不会按每个元素的优先级顺序返回数据。这是我正在使用的代码:
Firebase answersRef = new Firebase("/Questions/ModeratorQuestions/General/" + question.getKey() + "/Answers");
answersRef.orderByPriority().addListenerForSingleValueEvent(new ValueEventListener(
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println(dataSnapshot);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
以下是我在firebase中设置数据库的方法
-Questions
-ModeratorQuestions
-General
-QuestionIdNumber
-Answers
-Answer 1 (With priority of 0)
-Answer 2 (With priority of 1)
-Answer 3 (With priority of 2)
可以找到快照here。
当我尝试在我的代码中查询上面指定的url时,我从dataSnapshot获得的输出(我从url中取出了项目名称)。
{ key = Answers, value = {Douglas_Comer={.priority=0.0, .value=0}, Sophie_Dee={.priority=1.0, .value=0}} }
{ key = Answers, value = {Yes={.priority=0.0, .value=0}, No={.priority=1.0, .value=0}} }
{ key = Answers, value = {Go_on_a_shopping_spree_at_the_mall={.priority=1.0, .value=0}, Donate_it_to_charity={.priority=3.0, .value=0}, Stocks={.priority=4.0, .value=0}, Go_to_Walmart_I'll_figure_out_the_rest_when_I_get_there={.priority=2.0, .value=0}, DANCE={.priority=0.0, .value=0}} }
{ key = Answers, value = {Yes={.priority=0.0, .value=0}, Love_is_Dead={.priority=2.0, .value=0}, No={.priority=1.0, .value=0}} }
{ key = Answers, value = {three_landmines={.priority=1.0, .value=0}, 1,000_non-venomous_snakes={.priority=0.0, .value=0}} }
{ key = Answers, value = {have_free,_coach-seating,_air_travel_once_a_month={.priority=1.0, .value=0}, own_a_yacht={.priority=0.0, .value=0}} }
{ key = Answers, value = {Valentine's={.priority=3.0, .value=0}, Thanksgiving={.priority=2.0, .value=0}, Other={.priority=4.0, .value=0}, Halloween={.priority=1.0, .value=0}, Christmas={.priority=0.0, .value=0}} }
{ key = Answers, value = {Bread={.priority=0.0, .value=0}, Tortillas={.priority=3.0, .value=0}, Pasta={.priority=2.0, .value=0}, Rice={.priority=1.0, .value=0}} }
{ key = Answers, value = {fight_your_crush={.priority=0.0, .value=0}, fight_a_bear_cub={.priority=1.0, .value=0}} }
{ key = Answers, value = {Happy_relationship={.priority=0.0, .value=0}, Eligible_Bachelor={.priority=1.0, .value=0}, NULL_POINTER_EXCEPTION={.priority=2.0, .value=0}, Focused_on_beautiful_Science={.priority=4.0, .value=0}, I_have_no_idea_what_I'm_doing={.priority=3.0, .value=0}} }
{ key = Answers, value = {Apples={.priority=0.0, .value=0}, Bananas={.priority=1.0, .value=0}, Oranges={.priority=2.0, .value=0}} }
正如您所看到的,大部分都不按优先顺序排列。我不明白为什么。如果有人可以帮我解决我做错了什么,我们将不胜感激。如果您需要更多信息,我也可以回答问题。
答案 0 :(得分:0)
地图中的项目没有已定义的顺序。但是,可以从Firebase以正确的顺序检索项目,您可以验证是否循环使用它们:
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
System.out.println(child.getValue());
}
}
查看相同的问题,然后查看iOS:Issue displaying array of image from url