有没有办法重命名firebase中的子节点名称?

时间:2016-04-14 17:45:22

标签: android firebase firebase-realtime-database

{
  "750bf295-1a99-4b0f-a072-cae87d25f53e" : {
    "email" : "aa@aa.aa",
    "hotel" : {
      "Vkci" : {
        "author" : "Jole",
        "comfort" : 2.5,
        "food" : 4.0,
        "latit" : 45.545908325164106,
        "longit" : 18.40588353574276,
        "name" : "Vkci",
        "rating" : 3.6666667461395264,
        "review" : "nije lose nije ni dobro... sve u svem ok buggy. ff craft Jews",
        "service" : 4.5
      }
    },
    "password" : "aa",
    "username" : "Jole"
  },
  "ad08b893-a32c-4a2e-8003-2ac3eea2cda5" : {
    "email" : "ee@ee.ee",
    "hotel" : {
      "New" : {
        "author" : "ee",
        "comfort" : 3.0,
        "food" : 4.0,
        "latit" : 27.64581665792199,
        "longit" : -12.49478705227375,
        "name" : "New",
        "rating" : 3.0,
        "review" : "OK je",
        "service" : 2.0
      },
      "Paris" : {
        "author" : "ee",
        "comfort" : 3.0,
        "food" : 2.5,
        "latit" : 48.88729367986915,
        "longit" : 2.047065943479538,
        "name" : "Pariss",
        "rating" : 3.1666667461395264,
        "review" : "hard high Sheffield Hogg NCAA CFC",
        "service" : 4.0
      },
      "Pariz" : {
        "author" : "ee",
        "comfort" : 3.0,
        "food" : 2.5,
        "latit" : 48.88729367986915,
        "longit" : 2.047065943479538,
        "name" : "Parize",
        "rating" : 3.1666667461395264,
        "review" : "hard high Sheffield Hogg NCAA CFC",
        "service" : 4.0
      }
    },
    "password" : "ee",
    "username" : "ee"
  }
}

这是我的firebase数据库JSON。

我从我的适配器调用MapActivity并且我传递了这些数据:

 @Override
        public void onClick(View v) {

            int position = getAdapterPosition();
            Hotel hotel = this.hotels.get(position);
            Intent intent = new Intent(this.ctx, MyHotelDetailsMapActivity.class);
            intent.putExtra("author", hotel.getAuthor());
            intent.putExtra("food", String.valueOf(hotel.getFood()));
            intent.putExtra("service", String.valueOf(hotel.getService()));
            intent.putExtra("comfort", String.valueOf(hotel.getComfort()));
            intent.putExtra("name", hotel.getName());
            intent.putExtra("review", hotel.getReview());
            intent.putExtra("latit", String.valueOf(hotel.getLatit()));
            intent.putExtra("longit", String.valueOf(hotel.getLongit()));
            intent.putExtra("rating", String.valueOf(hotel.getRating()));
            this.ctx.startActivity(intent);
        }

在我的MapActivity中,我有一个对话框来编辑一些数据或删除酒店:

 @Override
    public boolean onMarkerClick(Marker marker) {
        final double lat = Double.valueOf(getIntent().getStringExtra("latit"));
        final double lon = Double.valueOf(getIntent().getStringExtra("longit"));

        LayoutInflater inflater = LayoutInflater.from(MyHotelDetailsMapActivity.this);
        View contentView = inflater.inflate(R.layout.dialog_edit, null);
        final EditText hotelName = (EditText) contentView.findViewById(R.id.hotel_name_edittext);
        final RatingBar ratingBarFood = (RatingBar) contentView.findViewById(R.id.hotel_ratingbar_food);
        final RatingBar ratingBarService = (RatingBar) contentView.findViewById(R.id.hotel_ratingbar_service);
        final RatingBar ratingBarComfort = (RatingBar) contentView.findViewById(R.id.hotel_ratingbar_comfort);
        final EditText comment = (EditText) contentView.findViewById(R.id.comment);

        hotelName.setText(getIntent().getStringExtra("name"));
        ratingBarFood.setRating(Float.valueOf(getIntent().getStringExtra("food")));
        ratingBarService.setRating(Float.valueOf(getIntent().getStringExtra("service")));
        ratingBarComfort.setRating(Float.valueOf(getIntent().getStringExtra("comfort")));
        comment.setText(getIntent().getStringExtra("review"));

        final Dialog dialog = new AlertDialog.Builder(MyHotelDetailsMapActivity.this)
                .setTitle("edit")
                .setView(contentView)
                .setNegativeButton(android.R.string.cancel, null)
                .setPositiveButton("save", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        float averageRating = ((ratingBarComfort.getRating() + ratingBarFood.getRating() + ratingBarService.getRating()) / 3);
                        String hotelUid = getIntent().getStringExtra("name");
                        String uid = firHotRef.getAuth().getUid();
                        autName = getIntent().getStringExtra("author");
                            final Firebase hotelsRef = new Firebase("https://josip-my-application.firebaseio.com/Users/" + uid + "/hotel/").child(hotelUid);

                            hotelsRef.setValue(rateHotel(hotelName.getText().toString(),
                                lat, lon, autName, ratingBarFood.getRating(), ratingBarService.getRating(),
                                ratingBarComfort.getRating(), averageRating, comment.getText().toString()));

                            dialog.dismiss();
                    }
                })
                .setNeutralButton("delete", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String hotelUid = getIntent().getStringExtra("name");
                        String uid = firHotRef.getAuth().getUid();
                        final Firebase hotelsRef = new Firebase("https://josip-my-application.firebaseio.com/Users/" + uid + "/hotel/").child(hotelUid);
                        hotelsRef.removeValue();
                        dialog.dismiss();
                        Intent i = new Intent(MyHotelDetailsMapActivity.this, ListActivity.class);
                        i.putExtra("username", getIntent().getStringExtra("author"));
                        startActivity(i);
                    }
                })
                .create();

        dialog.show();
        return true;
    }

    public Map<String, Object> rateHotel(String name, double lat, double lon, String author,float foodRating,float serviceRating,float comfortRating,float averageRating, String review) {
        Map<String, Object> hotel = new HashMap<>();
        hotel.put("name", name);
        hotel.put("latit", lat);
        hotel.put("longit", lon);
        hotel.put("author", author);
        hotel.put("food", foodRating);
        hotel.put("service", serviceRating);
        hotel.put("comfort", comfortRating);
        hotel.put("rating", averageRating);
        hotel.put("review", review);
        return hotel;
    }

当我点击肯定按钮保存更改时,如果我将酒店名称保留在适配器中,则所有其他字段都会更新,但如果我重命名酒店名称,我会得到不同的子节点名称,就像在我的JSON中一样(我有子节点名称巴黎,但我将酒店名称重命名为Pariss),所以当我点击中性按钮删除该酒店时,无法找到...有没有办法重命名子节点名称,在这种情况下,如果我将巴黎重新命名为Pariss,那儿童节点也变成了Pariss?

0 个答案:

没有答案
相关问题