I am trying to get number of children.
public int get_number_of_children_place() {
myRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
lastRow=(int) dataSnapshot.getChildrenCount();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
myRef.child("0").child("name").setValue("3f");
return lastRow;
{
lastRow and myRef is defined as global. the problem is this code do not work if called from onCreate method... it works perfectly when called on click event. The question is how to make it work on oncreate method... thankyou
public class addPlaces extends AppCompatActivity {
int lastRow=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_places);
myRef = FirebaseDatabase.getInstance().getReference().child("place");
int rowNo=get_number_of_children_place();// this says rowNo = 0
}
public void on_press(View view){
int rowNo=get_number_of_children_place();// this says rowNo = 5(number of children)
}