如何在firebase数据库中为树自动增加索引id



这是我写的代码,但它不起作用。

EditText plant = (EditText)findViewById(R.id.plantname);
String pn = plant.getText().toString();
FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();


FirebaseDatabase database = FirebaseDatabase.getInstance();
myref  = database.getReference("Plants").child(String.valueOf(id+1));

myref.child("Name").setValue(pn);
myref.child("Latitude").setValue(lats);
myref.child("Longitude").setValue(longs);

我想多次添加这三个细节,但索引不同。

myref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
id = snapshot.getChildrenCount();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
EditText plant = (EditText)findViewById(R.id.plantname);
String pn = plant.getText().toString();
FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();


FirebaseDatabase database = FirebaseDatabase.getInstance();
myref  = database.getReference("Plants");

myref.child(System.currentTimeMillis()).child("Name").setValue(pn);
myref.child(System.currentTimeMillis()).child("Latitude").setValue(lats);
myref.child(System.currentTimeMillis()).child("Longitude").setValue(longs);

最新更新