如何在adnroid中设置内部接口内部全局变量的值并使用接口外部的值

发布于 2025-01-17 15:16:10 字数 903 浏览 3 评论 0原文

有什么方法可以在接口内部设置全局变量的值并在接口外部使用该值吗?

这是我的代码示例:

class A{
static ArrayList<String> myList = new ArrayList<>();
public static void main(String[] args) {
    get(new Inter() {
        @Override
        public void callBack(ArrayList<String> list) {
            myList = list; // here list is the textual data from firebase and I want to use it outside
            //  this get function call
        }
    });
    myList.toString(); // I want to use it here. But its value out of the function call was null
                       // but it has the same value with list inside the function call
}
static void get(Inter inter){
    // here I want to get some textual data from firebase into ArrayList of String
    list = array of String from firebase
    inter.callBack(list);
}
interface Inter{
    void callBack(ArrayList<String> list);
}

}

Is there any way to set value of global variable inside an interface and use the value outside of it?

Here is sample of my code:

class A{
static ArrayList<String> myList = new ArrayList<>();
public static void main(String[] args) {
    get(new Inter() {
        @Override
        public void callBack(ArrayList<String> list) {
            myList = list; // here list is the textual data from firebase and I want to use it outside
            //  this get function call
        }
    });
    myList.toString(); // I want to use it here. But its value out of the function call was null
                       // but it has the same value with list inside the function call
}
static void get(Inter inter){
    // here I want to get some textual data from firebase into ArrayList of String
    list = array of String from firebase
    inter.callBack(list);
}
interface Inter{
    void callBack(ArrayList<String> list);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱*していゐ 2025-01-24 15:16:10

get()呼叫之后,您不能直接使用列表,因为get呼叫是异步并且尚未完成的。
您可以在接口之外创建一个方法,该方法将列表作为参数以参数为例:

public static void main(String[] args) {
    get(new Inter() {
        @Override
        public void callBack(ArrayList<String> list) {
            doSomethingWithList(list);
        }
    });
    
}

private static void doSomethingWithList(List<Sting> list){
    //you code here
}

或者您可以使用方法 - 参考:

public static void main(String[] args) {
    get(<your class>::doSomethingWithList);
}

private static void doSomethingWithList(List<Sting> list){
    //you code here
}

You can't use the list directly after the get() call, because the get call is async and has not yet finished.
You can create a method outside the interface that takes the list as a parameter:

public static void main(String[] args) {
    get(new Inter() {
        @Override
        public void callBack(ArrayList<String> list) {
            doSomethingWithList(list);
        }
    });
    
}

private static void doSomethingWithList(List<Sting> list){
    //you code here
}

Or you can use method-reference:

public static void main(String[] args) {
    get(<your class>::doSomethingWithList);
}

private static void doSomethingWithList(List<Sting> list){
    //you code here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文