這是我在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來輕鬆檢索數據,如果我將使用它。任何想法?當我按下按鈕時會增加計數器嗎? –