【问题标题】:How to get specific child from Firebase database如何从 Firebase 数据库中获取特定的孩子
【发布时间】:2016-08-29 00:57:25
【问题描述】:

我有上述层次结构-

我想获得对带有红色下划线的对象的引用。

这是我的代码:

 mRootRefTravel = new Firebase("https://myfirebase.firebaseio.com/");
    mRootRefTravel.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.v("START_TRAVEL", "data: " + dataSnapshot.getValue());
            if (dataSnapshot.exists()) {
                for (DataSnapshot firstSnapShot : dataSnapshot.getChildren()) {

                    for (DataSnapshot secondSnapShot : firstSnapShot.getChildren()) {
                        Log.v("START_TRAVEL", "children: " + secondSnapShot.getValue());
                        Log.v("START_TRAVEL", "children: Trip: "+ secondSnapShot.child("Trip").getValue());

                        travel.setAccommodationCost((long) secondSnapShot.child("AccommodationCost").getValue());
                    }
                }

            }
            adapterStartTravel = new AdapterStartTravel(list, getActivity());
            mRecyclerView.setAdapter(adapterStartTravel);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

这是我的日志:

08-27 08:11:34.661 5002-5002/com.amit.trawel V/START_TRAVEL: data: {Trip_0={Trip={TravelingExpenses=233, CarRent=78, PhoneCosts=130, NumOfTickets=3, NumOfCars=3, LodgingExpenses=986, TripId=1, Currency=shekel, AccommodationCost=16, FoodBudget=2100, NumOfRoom=244, NumOfPeople=3, AttractionsBudge=1100, OtherExpenses=555, FlightTicketTotalCost=500, ShoppingExpenses=200, NumOfDaysInsurance=569, LodgingBudget=1200, TotalTripSum=40000, ShoppingBudget=350, InsuranceCost=250, AttractionsExpenses=2, NumOfNightsAccommodation=2, TravelingBudget=470, NumOfDaysCarRent=1, OtherExpensesBudget=300, Destination=israel, FoodExpenses=5}}, Trip_1={Trip={TravelingExpenses=666, CarRent=60, PhoneCosts=100, NumOfTickets=4, NumOfCars=4, LodgingExpenses=78, TripId=2, Currency=dinar, AccommodationCost=40, FoodBudget=3000, NumOfRoom=44, NumOfPeople=2, AttractionsBudge=1500, OtherExpenses=555, FlightTicketTotalCost=400, ShoppingExpenses=300, NumOfDaysInsurance=77, LodgingBudget=1300, TotalTripSum=50000, ShoppingBudget=220, InsuranceCost=220, AttractionsExpenses=3, NumOfNightsAccommodation=3, TravelingBudget=500, NumOfDaysCarRent=2, OtherExpensesBudget=200, Destination=jordan, FoodExpenses=127}, Purchase_0={Lat=0.0, SumSpent=0, Lng=0.0, TripID=1, DBRowID=1}}}

08-27 08:11:34.662 5002-5002/com.amit.trawel V/START_TRAVEL: children: {TravelingExpenses=233, CarRent=78, PhoneCosts=130, NumOfTickets=3, NumOfCars=3, LodgingExpenses=986, TripId=1, Currency=shekel, AccommodationCost=16, FoodBudget=2100, NumOfRoom=244, NumOfPeople=3, AttractionsBudge=1100, OtherExpenses=555, FlightTicketTotalCost=500, ShoppingExpenses=200, NumOfDaysInsurance=569, LodgingBudget=1200, TotalTripSum=40000, ShoppingBudget=350, InsuranceCost=250, AttractionsExpenses=2, NumOfNightsAccommodation=2, TravelingBudget=470, NumOfDaysCarRent=1, OtherExpensesBudget=300, Destination=israel, FoodExpenses=5}

08-27 08:11:34.662 5002-5002/com.amit.trawel V/START_TRAVEL: children: Trip: null

这是我得到的例外-

java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference

这条线例外——

travel.setAccommodationCost((long) secondSnapShot.child("AccommodationCost").getValue());

我希望只有在创建更高的孩子之后才能接触到这个孩子。这就是为什么我不能像这样将最终路径引用为硬编码路径:

 mRootRefTravel = new Firebase("https://myfirebase.firebaseio.com/Trip_0/Trip");

【问题讨论】:

  • 请提供更多的堆栈跟踪。哪条线路有问题?
  • 请编辑您的问题以包含该信息
  • 你的图片没有显示“AccommodationCost”,所以我不确定你想问什么
  • “AccommodationCost”值位于“Trip”子项中。如果我可以到达“Trip”child(),我可以检索这个值
  • secondSnapShot.getValue() 不是对该值的引用吗?例如,secondSnapShot.child("Trip").getValue() 返回 null,因为第二个快照中没有“Trip”元素。

标签: android firebase firebase-realtime-database


【解决方案1】:

正如@cricket_007 建议的那样,我使用 secondSnapShot.getValue() 获得了对所需 child() 的引用——比我应该在 JSON 树中挖掘更进一步。

我需要这样写我的 getValue():

travel.setAttractionsBudget((long) firstSnapShot.child("Trip").child("AccommodationCost").getValue());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多