Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将数据推入firebase而不覆盖_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java 如何将数据推入firebase而不覆盖

Java 如何将数据推入firebase而不覆盖,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我想在firebase实时数据中插入这些注释。。如何生成键(Note1、Note2、Note3…)并将其与所附图片一起推送。。。我还尝试生成随机键,但它总是覆盖我不想要的数据 String note = ETNote.getText().toString(); DatabaseReference noteRef = FirebaseDatabase.getInstance().getReference().child("Users").child(userID).child("Notes");

我想在firebase实时数据中插入这些注释。。如何生成键(Note1、Note2、Note3…)并将其与所附图片一起推送。。。我还尝试生成随机键,但它总是覆盖我不想要的数据

String note = ETNote.getText().toString();
DatabaseReference noteRef = FirebaseDatabase.getInstance().getReference().child("Users").child(userID).child("Notes");
String noteID = noteRef.push().getKey();
Map newPost = new HashMap();
newPost.put(noteID, note);
noteRef.setValue(newPost);
Toast.makeText(HomeActivity.this, "Note Saved", Toast.LENGTH_LONG).show();

试试这个:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Notes").push();
ref.child("note1").setValue(notes1);
ref.child("note2").setValue(notes2);
ref.child("note3").setValue(notes3);
然后你将有:

Notes
  randomid
      note1: notes
      note2: notes
      note3: notes
有两种方法

第一种方法是直接寻址每个子节点,类似于Peter在回答中所示:

noteRef.push().setValue(newPost);
另一种方法是创建(可能有多个)新注释的映射,然后更新
noteRef

String noteID = noteRef.push().getKey();
Map newPost = new HashMap();
newPost.put(noteID, note);
noteRef.updateChildren(newPost);

很抱歉我想我不清楚。。我不知道我会有多少笔记。。这就是为什么我需要生成Note1,Note2,Note3。。等等用程序。。。就像当我按下一个便笺时,它会自动创建子便笺(便笺1),下一次是便笺2。。如此类推,为什么注释1和注释2会这样做?只需用randomid分隔注释,就可以得到notes/randomid/note:details/randomid/note:details。。此外,您还可以添加笔记以外的其他详细信息。这看起来像一组笔记。Firebase强烈建议不要使用顺序,增加索引。看见