Firebase实时数据库映射到具有多个子类的类

时间:2017-04-09 16:52:04

标签: android firebase firebase-realtime-database

Firebase实时数据库 1

上面的图片是我想从数据库中检索它并将其映射到我的android studio中的类的数据。但是会出现警告:

W/ClassMapper: No setter/field for indian found on class com.example.kangj.dabao.Stalls

我的课程如下:

CoffeeShop.java

public class CoffeeShop {
    private LatLng latlng;
    private String image;
    private double lat;
    private double lng;
    private String address;
    private String name;
    private int zipcode;
    private Map<String, Stalls> stalls;

    public CoffeeShop() {
    }

    public CoffeeShop(String i,double la,double lo,String a, String n, int z, Map<String, Stalls> s)
    {
        image = i;
        lat = la;
        lng = lo;
        address = a;
        name = n;
        zipcode = z;
        stalls = s;
        latlng = new LatLng(lat,lng);
    }

    public LatLng getLatLng() {


        latlng = new LatLng(lat,lng);
        return latlng;
    }

    public void setLatLng(LatLng latlng) {
        this.latlng = latlng;
    }

    public String getImage()
    {
        return image;
    }
    public void setImage(String i)
    {
        image = i;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getZipcode() {
        return zipcode;
    }

    public void setZipcode(int zipcode) {
        this.zipcode = zipcode;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLng() {
        return lng;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }

    public Map<String, Stalls> getStalls() {
        return stalls;
    }

    public void setStalls(Map<String, Stalls> stalls) {
        this.stalls = stalls;
    }
}

Stalls.java

public class Stalls {
    private Map<String, Food> rotiprataegg;
    private Map<String, Food> kwaychap;
    private Map<String, Food> nasilemak;
    private Map<String, Food> friedrice;

    public Stalls()
    {

    }
    public Stalls(Map<String,Food> rotiprataegg)
    {
        this.rotiprataegg = rotiprataegg;
    }
    public Map<String,Food>  getRotiprataegg() {
        return rotiprataegg;
    }

    public void setRotiprataegg(Map<String,Food>  rotiprataegg) {
        this.rotiprataegg = rotiprataegg;
    }
    public Map<String,Food>  getKwaychap() {
        return kwaychap;
    }

    public void setKwaychap(Map<String,Food>  kwaychap) {
        this.kwaychap = kwaychap;
    }

    public Map<String,Food>  getNasilemak() {
        return nasilemak;
    }

    public void setNasilemak(Map<String,Food>  zichar) {
       this.nasilemak = zichar;
    }

    public Map<String,Food>  getFriedrice() {
        return friedrice;
    }

    public void setFriedrice(Map<String,Food>  muslim) {
        this.friedrice = muslim;
    }
}

Food.java

public class Food {
    private String name;
    private String price;
    private String image;

    public Food()
    {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

}

更新

管理修复没有setter / field,现在又出现了另一个错误

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.kangj.dabao.Food
                                                                                   at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg$zza.zze(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg$zza.zzaH(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg$zza.zze(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg$zza.zzaH(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                                                   at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                                   at com.example.kangj.dabao.LocationActivity$1.onDataChange(LocationActivity.java:126)
                                                                                   at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是我从firebase获取数据的方式

ArrayList<CoffeeShop> coffeeShopsLists;

coffeeShopsLists = new ArrayList<CoffeeShop>();
        mDatabase = FirebaseDatabase.getInstance().getReference();
        mDatabase.child("restaurant").addListenerForSingleValueEvent(
                new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        // Get user value
                        for(DataSnapshot data : dataSnapshot.getChildren())
                        {
                            coffeeShopsLists.add(data.getValue(CoffeeShop.class));

                        }
                        buildGoogleApiClient();

                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
    }

0 个答案:

没有答案