如何从Firebase数据库中获取特定子项

时间:2016-08-27 12:52:37

标签: android firebase firebase-realtime-database

我有上述等级 - enter image description here

我希望获得以红色加下划线的对象的引用。

这是我的代码:

 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");

1 个答案:

答案 0 :(得分:0)

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

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

travel.setAttractionsBudget((long) firstSnapShot.child("Trip").child("AccommodationCost").getValue());
相关问题