0

I'm stuck with passing variable values outside the method. Example:

usernameRef = FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Name");
    usernameRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String createdByName = dataSnapshot.getValue().toString();
            }

and i want to use received in method value further: textview.setText(createdByName).

Also another example:

ServiceSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if(parent.getItemAtPosition(position).equals("Pick a service name")){

                }
                else{

                    String ServiceName = parent.getItemAtPosition(position).toString();
                }
            }

And outside the method i want to:

AddRequest items = new AddRequest(ServiceName);
FirebaseDatabase.getInstance().getReference("Requests").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(items)

As i undestand use local variable more correct, but i anyway dont know how to get this value outside the method.

QuqAuq
  • 21
  • 6
  • 2
    Sorry, your question isn't clear at all. Please dont *explain* what your code is supposed to be doing, instead: show more code, like a [mcve]. – GhostCat May 20 '20 at 07:35
  • 1
    maybe [something like this](https://stackoverflow.com/questions/57330766/how-to-get-data-from-any-asynchronous-operation-in-android) is what you're looking for – a_local_nobody May 20 '20 at 07:35
  • 3
    Beyond that: you want to learn about the difference between local variables and fields. – GhostCat May 20 '20 at 07:37
  • I think here enough code. Im get value from database in method and want to use this value outside the method (example in textview). But ok, i can add more – QuqAuq May 20 '20 at 07:49
  • 1
    You just need to define all those variable at top of your code. `string strName` and then define it's value wherever you want to. This way whole code will have access to the variable. – Dharmaraj May 20 '20 at 10:26
  • @Dharmaraj reply is solved my problem, i declared in class private string strName and now i can use variable value outside the method. Anyway thx yall – QuqAuq May 20 '20 at 10:45

1 Answers1

0

What you could do is create a new Java class named something like "GlobalVars"

in that class you could do something like

public static String value1 = "";
public static String value2 = "";
public static String value3 = "";
public static String value4 = "";

And in your mainclass

usernameRef = FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Name");
usernameRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            GlobalVars.value1 = dataSnapshot.getValue().toString();
        }

And the you can call that var anywhere in your project like so:

textview.setText(GlobalVars.value1).
  • this doesn't solve the problem. the call to the database is not synchronous. it can take any amount of time to complete. using the variable before the value has returned from the server won't be useful – a_local_nobody May 20 '20 at 08:21
  • oh, yea, im thinking about it, that exactly what i want, except that i want to read data from database like name and use it in classes and methods. But as i understand i can read this data on auth and pass them to this class, right? – QuqAuq May 20 '20 at 08:23
  • this will not solve your problem @QuqAuq – a_local_nobody May 20 '20 at 08:29
  • @a_local_nobody yes, now im get it, i still cant pass value outside the method, so i need another way – QuqAuq May 20 '20 at 08:36
  • You could add a while loop to check if value == "" Thread.sleep(1000). Kinda a nasty way tho. Otherwise make a method of the code and only call the method once you have a value from the db – Kaedin Schouten May 20 '20 at 11:58