简体   繁体   中英

How do I auto increment and make index id for a tree in firebase database?

This is the code I have written, but It is not working.

    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);

I want to add these three details many times, but with different indexes.

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM