gpt4 book ai didi

android - 如何避免覆盖 Firebase 中的数据

转载 作者:搜寻专家 更新时间:2023-11-01 07:49:20 24 4
gpt4 key购买 nike

这是我在 Firebase 中添加记录的代码。外面有一个名为 restauCount 的变量,其值 (int) 为 1

public void sendMessage(){

int restauCount = 1;
String identifier ="Restaurant" + restauCount;
Firebase userRef = firebaseRef.child("Caloocan");
EditText nameInput = (EditText) findViewById(R.id.nameTxt);
String name = nameInput.getText().toString();

EditText locInput = (EditText) findViewById(R.id.locationTxt);
String location = locInput.getText().toString();

EditText typeInput = (EditText) findViewById(R.id.typeTxt);
String foodtype = typeInput.getText().toString();
if (!name.equals("")){
Map<String, String> caloocan = new HashMap<String, String>();
caloocan.put("resname", name);
caloocan.put("resloc", location);
caloocan.put("foodtype", foodtype);

Map<String, Map<String, String>> users = new HashMap<String, Map<String, String>>();
users.put(identifier,caloocan);

userRef.setValue(users);
restauCount++;
}
}

当我再次运行 sendessage() 时。我将在字段中键入内容,当我单击 ADD(即 sendMessage)时,它将添加到 FireBase 中,但是当我添加新数据时。它会覆盖输入的旧数据吗?如何在不覆盖数据的情况下在 Firebase 中添加多个数据?创建 restauCount 是为了增加我输入的餐厅数量,

最佳答案

userRef.push().setValue(users);每次向指定的 Firebase 引用添加新子项时,push() 方法都会生成一个唯一键

关于android - 如何避免覆盖 Firebase 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37549853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com