简体   繁体   中英

Why textview.setText() is working in a certain function?

I am try to set text of some textview present in fragment but when i set it through settext method in a method then it works fine, But when i do the same thing outside that method it gets failed BTW this all is happening in onViewCreated()

firebaseFirestore.addSnapshotListener((Activity) getContext(), new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {



            //   This all working


            Toast.makeText(getContext(), "Loading Data", Toast.LENGTH_SHORT).show();
            Profile_link_P = value.getString(HomePage.ImageUri);
            Name_P = value.getString(HomePage.NAME);
            Email_P = value.getString(HomePage.EMAIL);
            PhoneNumber_P = value.getString(HomePage.PHONENUMBER);
            Toast.makeText(getContext(), Name_P, Toast.LENGTH_SHORT).show();
            tv_Name_P.setText(Name_P);
            tv_Email_P.setText(Email_P);

            Picasso.get().load(Profile_link_P).resize(1000, 1000).centerCrop().into(img_ProfilePic_P);
        }
    });

    tv_PhoneNumber_P.setText(PhoneNumber_P); // This Thing is not working <<<<<<<<<<<<<<<<<<<<<<

What is the error you get? Does your app crash?

The only thing I understand here, without having the whole code, is that when you set your text (where it's not working), your variable PhoneNumber_P is null or empty.

You're calling addSnapshotListener , which is an async function: it may take some time to respond. This is why, inside, you have to code the onEvent function, which will be called once you'll get a response. But the execution of your code won't be suspended while you'll be waiting for your response.

The place where you call setText and where it's "not working" is called IMMEDIATELY after your call to addSnapshotListener , but the latest one may not have responded yet. So your variable is still null or empty, depending on how you initialized it.

So, to be clear: when you call asynchronous functions like addSnapshotListener , you have to think that the call may take 10 seconds or more, depending on the network quality, for example. The rest of your method will be executed immediately (like your "not working" setText ), and when you will get, at least, your response, the inner method will be called and you will get your values, in the end.

As a reminder, every method that contains Listener in its name will wait for some events (click, requests responses...) without blocking the rest of the code. And when the event is fired, then the inner method will be called.

Best solution is to move the static definition of the text to the fragment xml, as programming it infer as dynamic data and here it's a static one and thus deserves to be in the xml+string resource file

As far as I can see, you are writing an anonymous inner class. You cannot just write statements like textView.setText(). This particular structure written here only allows for your code to be the overriden method's body, not in the anonymous class body. If you can, try replacing it with a Lambda function.

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