如何为读/写Firebase创建类

时间:2018-07-11 16:56:07

标签: java android firebase firebase-realtime-database

如何创建模型以在firabase中读写查询并重用它们?

要了解在数据库Firebase中执行查询所发生的情况,并通过oncomplete返回我的结果,我只需要支持就可以了解android中模型的操作。

写,我也希望知道如何执行此方法。

public class ReadFirebaseData {
        private DatabaseReference db;
        private FirebaseAuth mfirebaseAuth;
        private FirebaseAuth.AuthStateListener authStateListener;
        private Gson gson;
        private SharedPreferences preferences;
        private SharedPreferences.Editor editor;
        private String jsonList;

        private static ReadFirebaseData INSTANCE;
}


   public static void readPlaces(final Context context, final OnComplete onComplete) {
   db.child("bdPlaces").addValueEventListener(new ValueEventListener() {
           @Override
           public void onDataChange(DataSnapshot dataSnapshot) {
               if (dataSnapshot.exists()) {
                   MyplacesList = new ArrayList<Myplaces>();
                   for (DataSnapshot snapshot:dataSnapshot.getChildren()) {
                       Myplaces myplaces = snapshot.getValue(Myplaces.class);
                       String idPlaces = snapshot.getKey();
                       myplaces.setIdPlaces(idPlaces);
                       MyplacesList.add(new Myplaces(
                               myplaces.getNamePlaces(),
                               myplaces.getMyLatitud(),
                               myplaces.getMyLongitud(),
                               myplaces.getMyOpen(),
                               myplaces.getPlacesCategory(),
                               myplaces.getIdPlaces(),
                               myplaces.getDescPlaces(),
                               myplaces.getRate(),
                               myplaces.getImage()
                       ));
                   }
                  Log.w("Guardando:", MyplacesList.get(0).getNamePlaces());
               }

               gson = new Gson();
               jsonList = gson.toJson(MyplacesList);

              onComplete.onRequestComplete(e, jsonList);

           }

           @Override
           public void onCancelled(DatabaseError databaseError) {

           }

       });
   }

1 个答案:

答案 0 :(得分:0)

看到您要使用的吸气剂,我建议您在Myplaces模型类中使用以下代码行:

public class Myplaces {
    String namePlaces, myOpen, placesCategory, idPlaces, descPlaces, rate, image;
    long myLatitud, myLongitud;

    public Myplaces() {}

    public Myplaces(String namePlaces, String myOpen, String placesCategory, String idPlaces, String descPlaces, String rate, String image, long myLatitud, long myLongitud) {
        this.namePlaces = namePlaces;
        this.myOpen = myOpen;
        this.placesCategory = placesCategory;
        this.idPlaces = idPlaces;
        this.descPlaces = descPlaces;
        this.rate = rate;
        this.image = image;
        this.myLatitud = myLatitud;
        this.myLongitud = myLongitud;
    }

    public String getNamePlaces() {
        return namePlaces;
    }

    public String getMyOpen() {
        return myOpen;
    }

    public String getPlacesCategory() {
        return placesCategory;
    }

    public String getIdPlaces() {
        return idPlaces;
    }

    public String getDescPlaces() {
        return descPlaces;
    }

    public String getRate() {
        return rate;
    }

    public String getImage() {
        return image;
    }

    public long getMyLatitud() {
        return myLatitud;
    }

    public long getMyLongitud() {
        return myLongitud;
    }
}

我假设您所有的字段均为String类型,只有myLatitudmyLongitud的类型为long。