简体   繁体   English

为什么 textview.setText() 在某个 function 中工作?

[英]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()我正在尝试设置片段中存在的一些 textview 的文本,但是当我通过 settext 方法在一个方法中设置它时它工作正常,但是当我在该方法之外做同样的事情时它会失败顺便说一句,这一切都发生在 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.在没有完整代码的情况下,我在这里唯一了解的是,当您设置文本(不起作用的地方)时,您的变量PhoneNumber_P为 null 或为空。

You're calling addSnapshotListener , which is an async function: it may take some time to respond.您正在调用addSnapshotListener ,这是一个异步 function:可能需要一些时间来响应。 This is why, inside, you have to code the onEvent function, which will be called once you'll get a response.这就是为什么在内部,您必须编写onEvent function,一旦您收到响应就会调用它。 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.在您调用addSnapshotListener之后,您调用setText及其“不工作”的位置会立即调用,但最新的可能尚未响应。 So your variable is still null or empty, depending on how you initialized it.所以你的变量仍然是 null 或空,这取决于你如何初始化它。

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.因此,要明确一点:当您调用诸如addSnapshotListener类的异步函数时,您必须考虑到调用可能需要 10 秒或更长时间,例如,取决于网络质量。 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.您的方法的 rest 将立即执行(就像您的“不工作” setText一样),并且当您至少获得响应时,将调用内部方法并最终获得您的值。

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.提醒一下,名称中包含Listener的每个方法都会等待一些事件(单击、请求响应...),而不会阻塞代码的 rest。 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最好的解决方案是将文本的 static 定义移动到片段 xml 中,因为编程它推断为动态数据,这里它是一个 static 资源,因此值得放在 xml 文件中

As far as I can see, you are writing an anonymous inner class.据我所知,您正在编写一个匿名的内部 class。 You cannot just write statements like textView.setText().你不能只写像 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.此处编写的这种特殊结构仅允许您的代码成为被覆盖方法的主体,而不是匿名 class 主体中。 If you can, try replacing it with a Lambda function.如果可以,请尝试用 Lambda function 替换它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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