Java 使用firebase发布的数据丢失

Java 使用firebase发布的数据丢失,java,android,Java,Android,我试图将一些数据插入firebase,但遇到以下错误,第二个数据丢失。代码如下: Firebase.setAndroidContext(this); Button btnSave = (Button) findViewById(R.id.btnSave); btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {

我试图将一些数据插入firebase,但遇到以下错误,第二个数据丢失。代码如下:

Firebase.setAndroidContext(this);

    Button btnSave = (Button) findViewById(R.id.btnSave);
    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {

                //Creating firebase object
                Firebase ref = new Firebase(Config.FIREBASE_URL);

                //Getting values to store
                String name = mName.getText().toString().trim();
                String address = mAddress.getText().toString().trim();
                String latlng = mLatLng.getText().toString().trim();

                //Creating Person object
                FM_Spots spots = new FM_Spots();

                //Adding values
                spots.setName(name);
                spots.setAddress(address);
                spots.setLatLng(latlng);

                //Storing values to firebase
                //ref.child("FM_Spots").setValue(spots);
                Firebase newRef = ref.child("FM_Spots").push();
                newRef.setValue(spots);


            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getMessage().toString(), Toast.LENGTH_SHORT).show();
            }
        }
    });
}
我只有两个字段,分别是name和address,其中包含来自最后一个字段(latlng)的数据。请告知,谢谢

-sea-

当您说“第二个数据”时,您的意思是可写对象的第二个字段丢失,对吗

如果是这样,可以使用updateChildren(Map Map)方法而不是setValue()来指定要直接写入的字段:

newRef.updateChildren(convertFMSpotsToMap(spots));
其中ConvertFmSpotosMap()如下所示:

private static Map<String, Object> convertFMSpotsToMap(@NonNull FM_Spots spots) {
   HashMap<String, Object> map = new HashMap<>();
   map.put("name", spots.getName());
   map.put("address", spots.getAddress());

   return map;
} 
private static Map convertfmspottomap(@NonNull FM_Spots){
HashMap=newHashMap();
map.put(“name”,spots.getName());
map.put(“address”,spots.getAddress());
返回图;
}