如何避免覆盖firebase中的数据 [英] How to avoid overwriting of data in firebase

查看:186
本文介绍了如何避免覆盖firebase中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Firebase中添加记录的代码。有一个名为restauCount(int)的可变外部类型为1 b / restauCount = 1;
字符串标识=餐厅+ 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);

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

userRef.setValue(users);
restauCount ++;




$ b $ p $当我再次运行sendessage()。我会输入字段,当我点击添加哪个是sendMessage它将被添加到FireBase,但是当我添加新的数据。它覆盖了老的数据输入?如何在不覆盖数据的情况下在火灾中添加多个数据?
restauCount是为了增加我输入的餐厅数量而创建的,您需要更新<$ c $

  int restauCount = 1; 
字符串标识=餐厅+ restauCount;

尝试类似:

  long restauCount = System.currentTimeMillis(); 
字符串标识=餐厅+ restauCount;

这里每次发送一个 sendMessage()你的标识符得到一个特定的ID作为当前时间以毫秒为单位+餐馆



如果保持int数字的重要性让我知道


This is my code for adding records in Firebase. there's variable outside called restauCount valued (int) as 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++;
    }
}

When i run the sendessage() again. i will type in the fields and when i click ADD which is the sendMessage it will be added in FireBase , however when i add new data. IT OVERWRITES THE OLD DATA INPUTTED ? HOW CAN I ADD MULTIPLE DATA IN FIREBASE WITHOUT OVERWRITING THE DATA? restauCount was created to increment the number of Restaurant i inputted,

解决方案

you need to update the identifier it stays the same :

int restauCount = 1;
String identifier ="Restaurant" + restauCount;

try something like :

    long restauCount = System.currentTimeMillis();
    String identifier ="Restaurant" + restauCount;

here each time you send a sendMessage() your identifier got a specific id as the current time in milliseconds + "Restaurant"

if its important to keep int numbers let me know

这篇关于如何避免覆盖firebase中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆