[英]How do I auto increment and make index id for a tree in firebase database?
這是我寫的代碼,但它不起作用。
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.