从Firebase DB检索失败的值,获取null而不是期望的String

时间:2017-04-15 13:00:59

标签: android json firebase

我在我的Android应用程序和FirebaseUI库中使用Firebase数据库。

enter image description here

我设计了数据模型(目前故意排除某些键):

/**
Retrieving CarPark object from Firebase db
 */

public class CarParkData {

    private String mTitle;
    private String mSponsor;
    private String mAddress;
    private String mDesc;

    // default parameters constructor required for Firebase for data retrieval
    public CarParkData() {

    }

    // constructor
    public CarParkData(String title, String sponsor, String description, String address1) {
        mTitle = title;
        mSponsor = sponsor;
        mAddress = address1;
        mDesc = description;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public String getSponsor() {
        return mSponsor;
    }

    public void setSponsor(String sponsor) {
        mSponsor = sponsor;
    }

    public String getAddress() {
        return mAddress;
    }

    public void setAddress(String address1) {
        mAddress = address1;
    }

    public String getDesc() {
        return mDesc;
    }

    public void setDesc(String description) {
        mDesc = description;
    }
}

使用了FirebaseListAdapter

mAdapter = new FirebaseListAdapter<CarParkData>(getActivity(), CarParkData.class,
               R.layout.test, ref) {

           @Override
           protected void populateView(View v, CarParkData model, int position) {
               ((TextView)v.findViewById(R.id.text_one)).setText(model.getTitle());
               ((TextView)v.findViewById(R.id.text_two)).setText(model.getAddress());
               ((TextView)v.findViewById(R.id.text_three)).setText(model.getSponsor());

               String test = model.getTitle();    // test: "Q-Park Victoria Square
               String test2 = model.getSponsor(); // test2: "false"
               String test3 = model.getAddress(); // test3: "null"
               String test4 = model.getDesc();    // test4: "null"

使用的JSON对象:

{
    "-KhguCEtT6TpPPmuvMvY": {
        "title": "Q-Park Victoria Square",
        "owner": "Q-Park",
        "address1": "Victoria Street 1",
        "address_line2": {
            "post_code": "BT1 4QG",
            "city": "Kekity"
        },
        "description": "Located in the heart of Kekistan, Q-Park Victoria Square has direct access to each floor of Victoria Square Mall",
        "image_url": "image_url",
        "parking_capacity": 1000,
        "available_spaces": 70,
        "max_vehicle_height": 2.1,
        "schedule": {
            "monday": {
                "24_7": true,
                "open": "null",
                "close": "null"
            },
            "tuesday": {
                "24_7": true,
                "open": "null",
                "close": "null"
            },
            "wednesday": {
                "24_7": true,
                "open": "null",
                "close": "null"
            },
            "thursday": {
                "24_7": true,
                "open": "null",
                "close": "null"
            },
            "friday": {
                "24_7": true,
                "open": "null",
                "close": "null"
            },
            "saturday": {
                "24_7": true,
                "open": "null",
                "close": "null"
            },
            "sunday": {
                "24_7": true,
                "open": "null",
                "close": "null"
            }
        },
        "pricing": {
            "24_7": true,
            "schedule": {
                "day": {
                    "hourly_rate": 3.00,
                    "start_time": "8:00",
                    "end_time": "19:00"
                },
                "night": {
                    "hourly_rate": 4.50,
                    "start_time": "8:00",
                    "end_time": "19:00"
                }
            },
            "24_7_pass": 18.00,
            "day_pass": "null",
            "night_pass": "null"
        },
        "free_until": 0,
        "disabled_access": true,
        "city_picker": "kekity",
        "sponsor": "false",
        "location": "null"
    }

getTitlegetSponsor返回预期值,而getAddressgetDesc在预期String时返回null。谁能识别出发生了什么?

1 个答案:

答案 0 :(得分:2)

模型中的getter / setter名称必须与数据库中的属性名称完全匹配。在您的数据库中,您使用address1description。这些属性的getter / setter必须是:

public String getAddress1() {
    return mAddress;
}

public void setAddress1(String address1) {
    mAddress = address1;
}

public String getDescription() {
    return mDesc;
}

public void setDescription(String description) {
    mDesc = description;
}