简体   繁体   中英

how can I store the data snapshot of a eventlistner of firebase database in a string

I am trying to store the value of a child node in a string for my firebase project. But when I go out of the scope the data is not stored anymore. please help me.. I am attaching the code snippet I want to store the location string into another string out of eventlistener.

private RecyclerView findFriendList;
private EditText searchET;
private String str = "";
private DatabaseReference usersRef;
private DatabaseReference userlocationRef;
private String address ="" ;
private  String locationDb ="";
private TextView getlocation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find_electrician);

    getlocation =findViewById(R.id.get_location_electrician);


    userlocationRef = FirebaseDatabase.getInstance().getReference("Users");

    userlocationRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot)
                {
                    if(dataSnapshot.exists())
                    {
                        String location = dataSnapshot.child("location").getValue().toString();
                        locationDb = location;
                        address =locationDb;
                        getlocation.setText(address);
                        Toast.makeText(FindElectricianActivity.this, address, Toast.LENGTH_SHORT).show();

                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });



   // Toast.makeText(this, address, Toast.LENGTH_SHORT).show();
    address = getlocation.getText().toString();

    usersRef = FirebaseDatabase.getInstance().getReference().child("Location").child(address).child("ServiceProvider").child("Electrician");

    FirebaseRecyclerOptions<Contacts> options = null;

    searchET = findViewById(R.id.search_user_text_electrician);
    findFriendList = findViewById(R.id.find_friends_list_electrician);
    findFriendList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));


    searchET.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charseq, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence charseq, int start, int before, int count) {
            if (searchET.getText().toString().equals("")) {
                Toast.makeText(FindElectricianActivity.this, "please write name to search", Toast.LENGTH_SHORT).show();

            } else {
                str = charseq.toString();
                onStart();
            }


        }


        @Override
        public void afterTextChanged(Editable charseq) {

        }
    });

}

Here I want to store the location string into address string. It is storing as long as I am inside the addValuEventListner exist. But outside it became null. I added Toast message to verify it. So please help me out

Firebase APIs are asynchronous and return immediately, before any data is available. The data in your class members will not be assigned until after the callback is invoked. Until that happens, they will just be null. It's your responsibility to only access those member variables until after the callback is invoked. This means that you should probably only do that inside the callback, or some other method that they invoke.

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