这是我在Firebase中添加记录的代码。有可变的外部称为restauCount价值(int)为1如何避免覆盖Firebase中的数据
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时,它将被添加到FireBase中,但是当我添加新数据时。它覆盖了旧数据输入?如何在不覆盖数据的情况下在火灾中添加多个数据? restauCount是为了增加我输入的餐厅数量,
我的代码的错误是在.put()不在我的柜台restau,对不起 –
你试过我的答案吗?,因为为了避免覆盖你需要的数据每次一个新的标识符 –
它的工作原理,但我需要一个唯一的ID来轻松检索数据,如果我将使用它。任何想法?当我按下按钮时会增加计数器吗? –